Testing for a file in python?

A[r]TA arta at NOSPAMx-stream.nl
Mon Jun 12 14:20:35 EDT 2000


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

try:
    open('filename')
except:
    print 'File does not exist'

A[r]TA


--
-Life can kick your ass, but all you got to do is stand up and move on...





More information about the Python-list mailing list