I encountered a very strange behavior in Python, a behavior that is not consistent.
...
except IOError as msg:
sys.exit("###ERROR IOError: %s" % (msg))
Usually this would get me a message like:
###ERROR IOError: [Errno 13] Permission denied: 'filename'
In same cases the above code is giving me a tuple
instead of a proper error message.
###ERROR IOError: (13, 'Permission denied')
This is very strange because in all case the exception come from the same python method, codecs.open(...)
What makes me wonder more about this is that if I remove the handling the exception will reach upper levels with the right text (full error message), always!
except IOError as msg:
print(msg)
raise msg
The above example will always print a complete message, like IOError: [Errno 13] Permission denied: u'filename'
.
Why is this happening and how do I prevent this, I don't want to give incomplete error messages to the users.
I wanted to reproduce this behavior in a test file, but I was not able to reproduce this outside the project.
I suspect that is has something to do with the usage of sys.exit()
because print(msg)
will give good result but sys.exit
not.