How to get the errno of an IOError? How to get the errno of an IOError? python python

How to get the errno of an IOError?


The Exception has an errno attribute:

try:    fp = open("nothere")except IOError as e:    print(e.errno)    print(e)


Here's how you can do it. Also see the errno module and os.strerror function for some utilities.

import os, errnotry:    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 more information on IOError attributes, see the base class EnvironmentError:


try:    fp = open("nothere")except IOError as err:    print err.errno     print err.strerror