By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Does this path exist?

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
s0****@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.
--
Jun 27 '08 #2

P: n/a
On May 28, 3:47 am, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
s0s...@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.
Thanks. So if OSError().errno == errno.ENOENT, then it means the path
doesn't exist? (What does "ENOENT" stan for?)
Jun 27 '08 #3

P: n/a
On May 28, 10:59*am, s0s...@gmail.com wrote:
On May 28, 3:47 am, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
s0s...@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.

Thanks. So if OSError().errno == errno.ENOENT, then it means the path
doesn't exist? (What does "ENOENT" stan for?)
http://books.google.co.za/books?id=M...142FEeNI&hl=en

ENOENT - Invalid Entry (file or directory)
Jun 27 '08 #4

P: n/a
On May 28, 4:59 am, s0s...@gmail.com wrote:
On May 28, 3:47 am, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
....
Thanks. So if OSError().errno == errno.ENOENT, then it means the path
doesn't exist? (What does "ENOENT" stan for?)
I always read it as Error NO ENTry
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.