File existence check with partial filename

Alf P. Steinbach alfps at
Mon Mar 15 05:24:46 CET 2010

* Sang-Ho Yun:
> I learned that I can check the existence of a file using
> os.path.isfile("filename").
> What if I need to check if there is a file that contains "HV" in the
> filename?  What should I do?

     from __future__ import print_function
     import os

     for filename in os.listdir( "." ):
         if "HV" in filename.upper():
             print( filename )

Cheers & hth.,

- Alf

More information about the Python-list mailing list