Detect file existance before open?

François Pinard pinard at
Mon Nov 1 19:45:21 CET 1999

"Gordon McMillan" <gmcm at> 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

More information about the Python-list mailing list