Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2 views
Skip to first unread message

Gregory Piñero

unread,
Jul 5, 2006, 5:25:50 PM7/5/06
to pytho...@python.org
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:
#now psuedo code because this is what I'm trying to figure out
if error.errno=='file doesn't exist':
do something
elif error.errno=='no permissions':
do something else
... and so on

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!

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

Mark Peters

unread,
Jul 5, 2006, 5:44:28 PM7/5/06
to
> 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

Error number picked at random:

>>> import errno
>>> print errno.errorcode.keys()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42,
10000, 10004, 10009, 10013, 10014, 10022, 10024, 10035, 10036, 10037,
10038, 10039, 10040, 10041, 10042, 10043, 10044, 10045, 10046, 10047,
10048, 10049, 10050, 10051, 10052, 10053, 10054, 10055, 10056, 10057,
10058, 10059, 10060, 10061, 10062, 10063, 10064, 10065, 10066, 10067,
10068, 10069, 10070, 10071, 10091, 10092, 10093, 10101]
>>> print errno.errorcode[10]
ECHILD

or

>>> import os
>>> print os.strerror(10)
No child processes

Hope this helps,
Mark Peters

Gregory Piñero

unread,
Jul 5, 2006, 5:47:54 PM7/5/06
to Mark Peters, pytho...@python.org
Thanks Mark, that does help, but what is this errno module? I mean,
does it apply to OSError or to IOError or both?

> --
> http://mail.python.org/mailman/listinfo/python-list

Mark Peters

unread,
Jul 5, 2006, 5:55:01 PM7/5/06
to

Gregory Piñero wrote:
> Thanks Mark, that does help, but what is this errno module? I mean,
> does it apply to OSError or to IOError or both?
My guess is that the IOError will return the underlying operating
system error, but that's just a guess (and no time to dig for the
answer right now)

Mark

Mark Peters

unread,
Jul 5, 2006, 6:07:10 PM7/5/06
to
A quick test:

>>> try:
f = open("foo","r")
except IOError, error:
print errno.errorcode[error.errno]

ENOENT

It looks to me like your if statement should be as simple as:

if error.errno == errno.ENOENT:
print os.strerror(error.errno)

Mark

Justin Azoff

unread,
Jul 5, 2006, 7:56:52 PM7/5/06
to
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

0 new messages