The errno module defines some symbolic error codes, such as enoent ("not Found") and Eperm ("No Permissions").
If you need to distinguish between different error codes, you can use the symbol name.
In Py2, the IOError exception provides a tuple containing two values, which are error codes and error messages, as follows:
errno
try:
fp = open (' no.such.file ')
ioerror, (Error, MSG):
Error = = Errno. Enoent:
' No such file '
error = = errno. Eperm:
' permission denied '
else:
msg
Or:
errno
try:
fp = open (' no.such.file ')
ioerror, E:
E.errno = = errno. Enoent:
' No such file '
E.errno = = errno. Eperm:
' permission denied '
else:
Error
The exception capture method in Py3 is slightly different, the exception instance contains the error code, the code is as follows:
errno
try:
fp = open (' no.such.file ')
e:
E.errno = = errno. Enoent:
print(' No such file ')
E.errno = = errno. Eperm:
print(' Permission denied ')
else:
print(e)