By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,550 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)
Jul 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 5 '06 #2

P: n/a
Thanks Mark, that does help, but what is this errno module? I mean,
does it apply to OSError or to IOError or both?

On 5 Jul 2006 14:44:28 -0700, Mark Peters <mp*******@gmail.comwrote:
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

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

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Jul 5 '06 #3

P: n/a

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

Jul 5 '06 #4

P: n/a
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

Jul 5 '06 #5

P: n/a
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

Jul 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.