Testing for a file in python?
Jeff
jam at quark.emich.edu
Mon Jun 12 20:43:49 EDT 2000
olczyk at interaccess.com (Thaddeus L. Olczyk) writes:
> How would you go about testing if a certain file exists in python?
> In most scripting languages you have the -f test:
>
> if -f filename do
> whatever_you_were_going_to_do
> fi
> else do
> print "Error" file not found
> fi
>
> stat doesn't work, it aborts if the file does not exist.
how about 'os.path.isfile()'? it returns a 0 if the file does not exist, and
a 1 if it does. my guess is this will also work under win32, but I have not
personally tested that platform.
import os
if os.path.isfile("filename"):
# whatever_you_were_going_to_do
pass
else:
print "Error" # file not found
more information regarding the 'os.path' module is here:
http://www.python.org/doc/current/lib/module-os.path.html
regards,
J
--
|| visit gfd <http://quark.emich.edu/>
|| psa member -- <http://www.python.org/psa/>
More information about the Python-list
mailing list