os.path.isfile with *.tar.gz

Tim Golden mail at timgolden.me.uk
Thu Mar 15 15:58:26 EDT 2007


Boudreau, Emile wrote:
> Hello All,
>         I'm new to Python and it looks like people that post here do get 
> a good answer back so I figured I'd try my luck.
> 
> I'm trying to check a directory to see if there is a file that has the 
> name "startOfString" + some version number + "inst.tar.gz" 
> (component-8.3.16-inst.tar.gz) The version number will change quite 
> frequently so I just want to check if there is a file with that format name.
> 
> I have tried variations of: os.path.isfile( os.path.join("C:\\temp\\", 
> "rqp-win32-app", "*.tar.gz"))
> but nothing seem to work. Does anyone have suggestions on how I could 
> get this to work?

Well os.path.isfile checks for the existence of one specific
file. Since there isn't a file called c:\temp\rqp-win32-app\*.tar.gz
it will return False. Take a look at the glob module. This will
return a list of files matching a pattern in the way you're
suggesting. Since a non-empty list is True, and an empty one
False in Python, something like the following code might
work: (untested)

<code>
import glob

if glob.glob (r"c:\temp\rqp-win32-app\*.tar.gz"):
   print "At least one file matched"
else:
   print "No files matched"

</code>

TJG



More information about the Python-list mailing list