Granularity of OSError

Ryan Kelly ryan at rfk.id.au
Fri Sep 18 19:17:21 EDT 2009


> > You can access the exception object which gives you greater detail.
> >
> > try:
> >     os.unlink(some_file)
> > except OSError, e:
> >     print e.errno
> >     print e.strerror
> >
> >     if e.errno == 2:
> >         pass
> >     else:
> >         raise
>
> I do this myself in a lot of places, almost exactly like this. It's
> slightly clearer to use 'if e.errno == errno.ENOENT' in my opinion,
> but, whatever.

In some cases it's also more correct.  While ENOENT is always 2, some
error codes differ between windows and posix.  In general it's better to
use the constants from the errno module.

   Ryan


-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
ryan at rfk.id.au        |  http://www.rfk.id.au/ramblings/gpg/ for details

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20090919/015d80e6/attachment.sig>


More information about the Python-list mailing list