Testing for a file in python?

j vickroy jvickroy at sec.noaa.gov
Mon Jun 12 13:40:49 EDT 2000


Try the exists() method in the os.path module as follows:

>>> import os.path
>>> print os.path.exists ('my_file')
0
>>>

"Thaddeus L. Olczyk" wrote:

> 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.




More information about the Python-list mailing list