Built-in Exceptions - How to Find Out Possible Errno's

Justin Azoff justin.azoff at gmail.com
Wed Jul 5 19:56:52 EDT 2006


Gregory Piñero wrote:
> Hi Guys,
>
> I'm sure this is documented somewhere, I just can't locate it.  Say I
> have this code:
>
> try:
> 	myfile=file('greg.txt','r')
> except IOError, error:
[...]
> So basically I'm looking for the document that tells me what possible
> errors I can catch and their numbers.
>
> I did find this but it doesn't have numbers and I can't tell if it's
> even what I'm looking for:
> http://docs.python.org/lib/module-errno.html
>
> Much thanks!

that IS the module you are looking for.

>>> help(errno)
[...]
DESCRIPTION
    The value of each symbol is the corresponding integer value,
    e.g., on most systems, errno.ENOENT equals the integer 2.
[...]
    ENODATA = 61
    ENODEV = 19
    ENOENT = 2
    ENOEXEC = 8

all those E* constants ARE the numbers.

furthermore, the object you get back from except has both the code and
the string already:
>>> e
<exceptions.IOError instance at 0xb7ddbfec>
>>> print e
[Errno 2] No such file or directory: 'foo'
>>> dir(e)
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args',
'errno', 'filename', 'strerror']
>>> e.strerror
'No such file or directory'

-- 
- Justin




More information about the Python-list mailing list