How to use sys.exc_info()???
Alex Martelli
aleaxit at yahoo.com
Wed Oct 18 16:38:00 EDT 2000
"Roy Smith" <roy at panix.com> wrote in message
news:8sksit$mj1$1 at panix6.panix.com...
[snip]
> try:
> open ('xxx')
> except:
> foo = sys.exc_info()
>
> What I end up with in foo is
>
> (<class exceptions.IOError at 80a7e20>, <exceptions.IOError instance at
80c82d0>, <traceback object at 80c92c8>)
>
> What I don't understand is how to go from the 3-tuple I get back from
> exc_info() to the string "No such file or directory"?
>>> try: open('xxx')
except: foo=sys.exc_info()
>>> foo
(<class exceptions.IOError at 00805E0C>, <exceptions.IOError instance at
00B303FC>, <traceback object at 00B35E80>)
>>> foo[1]
<exceptions.IOError instance at 00B303FC>
>>> dir(foo[1])
['args', 'errno', 'filename', 'strerror']
>>> foo[1].args
(2, 'No such file or directory')
>>> foo[1].errno
2
>>> foo[1].filename
'xxx'
>>> foo[1].strerror
'No such file or directory'
>>>
So, specifically, what you want is foo[1].strerror, but there's other
info in the foo[1] instance which you may want to access (and if
you're just testing to see if the specific error is such-and-such,
foo[1].errno may be handier).
Alex
More information about the Python-list
mailing list