Detect file existance before open?

François Pinard pinard at iro.umontreal.ca
Mon Nov 1 13:45:21 EST 1999


"Gordon McMillan" <gmcm at hypernet.com> writes:

> > I would like to check for the existance of a file before I
> > attempt an open and maybe throw an exception.

> os.path.exists(nm), probably also os.path.isfile(nm).

And also os.path.islink(nm), unfortunately.

If a symbolic link exists and points to a deleted file, say, os.path.exists
returns None.  I found out that you need to test both os.path.exists and
os.path.islink before assuming the path is free for safe re-use.

It is a bit of constant annoyance in some projects, and do not clearly see
how Python `os' library could be improved to make this more transparent.
I guess we just have to learn to live with this (?) ...

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list