Does this path exist?

Matt Nordhoff mnordhoff at mattnordhoff.com
Wed May 28 04:47:03 EDT 2008


s0suk3 at gmail.com wrote:
> I wanted to ask for ways to test whether a path exists. I usually use
> os.path.exists(), which does a stat call on the path and returns True
> if it succeeds, or False if it fails (catches os.error). But stat
> calls don't fail only when a path doesn't exist. I see that, at least
> on Windows, the instance of the exception has an attribute 'errno' set
> to 2 when it fails because the path doesn't exist. Is it a portable
> solution to rely on this (haven't tried it on Linux)? Are there other
> ways of testing whether a path exists?
> 
> Thanks,
> Sebastian

"import errno" and see if the exception's errno attribute is set to
errno.ENOENT (which is, yes, 2). It is portable.

If you Google [ENOENT Windows] or whatever, there are some differences
on different platforms, but not many.
-- 



More information about the Python-list mailing list