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

  • Thread starter =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
  • Start date
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

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!
 
M

Mark Peters

I did find this but it doesn't have numbers and I can't tell if it's

Error number picked at random:
[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
No child processes

Hope this helps,
Mark Peters
 
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

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

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:
[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
 
M

Mark Peters

Gregory said:
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
 
M

Mark Peters

A quick test:
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
 
J

Justin Azoff

Gregory said:
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.
[...]
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:
print e [Errno 2] No such file or directory: 'foo'
dir(e)
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args',
'errno', 'filename', 'strerror']'No such file or directory'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top