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)
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
Mark
>>> 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
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