I'm trying to handle IOError generated when trying to open a non existent file. I do:
try:
inputFile = open('nosuchfile', 'r')
except IOError as exception:
print 'error: %s' % (exception)
This gives me:
error: [Errno 2] No such file or directory: 'nosuchfile'
But I'm only interested in the message and not the [Errno 2] part. So I change my code.
print 'error: %s' % (exception.strerror)
But now I get:
error: No such file or directory
Where did the name of the file go? I know I could just print the file name separately, but I would really like how (if at all) the name was stored in the exception, but not in either of its arguments (printing exception.errno gives 2).
I am using version 2.7.3.