[ACCEPTED]-How to get the errno of an IOError?-ioerror
Accepted answer
The Exception has an errno
attribute:
try:
fp = open("nothere")
except IOError as e:
print(e.errno)
print(e)
0
Here's how you can do it. Also see the 3 errno
module and os.strerror
function for some utilities.
import os, errno
try:
f = open('asdfasdf', 'r')
except IOError as ioex:
print 'errno:', ioex.errno
print 'err code:', errno.errorcode[ioex.errno]
print 'err message:', os.strerror(ioex.errno)
For 2 more information on IOError attributes, see 1 the base class EnvironmentError:
try:
fp = open("nothere")
except IOError as err:
print err.errno
print err.strerror
0
Different exceptions can also be tested 1 for in the same 'except' clause, e.g.
try:
serial_port.open()
except (AttributeError, SerialException) as e:
print('cannot open serial port: {}'.format(e))
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.