FAQ
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

Search Discussions

  • Matt Nordhoff at May 28, 2008 at 8:47 am

    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.
    --
  • S0suk3 at May 28, 2008 at 8:59 am

    On May 28, 3:47 am, Matt Nordhoff wrote:
    s0s... 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.
    Thanks. So if OSError().errno == errno.ENOENT, then it means the path
    doesn't exist? (What does "ENOENT" stan for?)
  • Chris at May 28, 2008 at 9:25 am

    On May 28, 10:59?am, s0s... at gmail.com wrote:
    On May 28, 3:47 am, Matt Nordhoff wrote:


    s0s... 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.
    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=MvoetCEgMRMC&pg=PA398&lpg=PA398&dq=enoent+meaning&source=web&ots1Ik2wY4NZ&sig=NUvvA1LTMDG0x4XK0x7142FEeNI&hl=en

    ENOENT - Invalid Entry (file or directory)
  • Chris Hulan at May 28, 2008 at 2:34 pm

    On May 28, 4:59 am, s0s... at gmail.com wrote:
    On May 28, 3:47 am, Matt Nordhoff wrote: ...
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 28, '08 at 7:58a
activeMay 28, '08 at 2:34p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase