FAQ
I'm new at Python coding and am trying to catch
errors returned by os.open with exception handling.
All errors with os.open are returned with an OSError
exception. So, how do I split out the individual errors
with something as simple as:

try:
file=os.open("test",os.O_RDWR | os.O_CREAT | os.O_EXCL, 0444)

except OSError:
{want to do different things here for errno EEXIST (file exists)
or EACCES (Permission denied), etc.}

I have looked at the tuple returned by sys.exc_info() and can
extract the *text* of the Python error message
(i.e. "[Errno 17] File exists"), but not the numeric error number.
I certianly don't want to have to rely on parsing the text to get
the actual error. I'm sure there is an easy answer to this if
someone would be kind enough to help me.

Thanks,

Mike
mblatch at orci.com

Search Discussions

  • Thomas Wouters at Jul 6, 2000 at 8:30 pm

    On Thu, Jul 06, 2000 at 10:03:44AM -0600, m wrote:

    I'm new at Python coding and am trying to catch
    errors returned by os.open with exception handling.
    All errors with os.open are returned with an OSError
    exception. So, how do I split out the individual errors
    with something as simple as:
    try:
    file=os.open("test",os.O_RDWR | os.O_CREAT | os.O_EXCL, 0444)

    except OSError:
    {want to do different things here for errno EEXIST (file exists)
    or EACCES (Permission denied), etc.}
    I have looked at the tuple returned by sys.exc_info() and can
    extract the *text* of the Python error message
    (i.e. "[Errno 17] File exists"), but not the numeric error number.
    I certianly don't want to have to rely on parsing the text to get
    the actual error. I'm sure there is an easy answer to this if
    someone would be kind enough to help me.
    You're looking for the 'errno' attribute of OSError objects. You should
    compare them to constants defined in errno, for compatibility, like so:


    import os, errno

    try:
    file = os.open("test", ... )
    except OSError, e:
    if e.errno == errno.EEXIST:
    print "The file eixsts already"
    elif e.errno == errno.EACCES:
    print "Can't touch this"
    elif e.errno == ELOOP:
    print "symlink after " * 1024 + "symlink!"
    <etc>


    --
    Thomas Wouters <thomas at xs4all.net>

    Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 6, '00 at 4:03p
activeJul 6, '00 at 8:30p
posts2
users2
websitepython.org

2 users in discussion

Thomas Wouters: 1 post M: 1 post

People

Translate

site design / logo © 2022 Grokbase