how do i check if a file exists with python?
Moshe Zadka
moshez at zadka.site.co.il
Fri Dec 29 09:54:56 EST 2000
[Scott Hathaway, on how to find a file]
> Thanks for both responses!
Unfortunately, I think they did you more harm then good by telling you
the answer: I doubt you need it. Now, it may be that I'm wrong, in which
case just go ahead an use os.path.isfile, but if you're doing something
like
def foo():
if os.path.isfile("foo"):
return open("foo")
else:
return None
Or something to that effect, *don't*. It's evil, and will come back
to bite you (unreadable existing files, files changing between isfile
and open, etc.).
Instead, simple do:
def foo():
try:
return open("foo")
except IOError:
return None
In other words, don't try to avoid exceptions -- know where you want
to deal with them, and do so.
--
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus.
Please stop the spread of signature viruses!
More information about the Python-list
mailing list