| I l@ve RuBoard |
|
2.12 The errno ModuleThe errno module defines a number of symbolic error codes, such as ENOENT ( "no such directory entry") and EPERM ( "permission denied"). It also provides a dictionary mapping from platform-dependent numerical error codes to symbolic names. Example 2-21 shows how to use errno. In most cases, the IOError exception provides a 2-tuple with the numerical error code and an explanatory string. If you need to distinguish between different error codes, use the symbolic names where possible. Example 2-21. Using the errno Module
File: errno-example-1.py
import errno
try:
fp = open("no.such.file")
except IOError, (error, message):
if error == errno.ENOENT:
print "no such file"
elif error == errno.EPERM:
print "permission denied"
else:
print message
no such file
Example 2-22 is a bit contrived, but it shows how to use the errorcode dictionary to map from a numerical error code to the symbolic name. Example 2-22. Using the errorcode Dictionary
File: errno-example-2.py
import errno
try:
fp = open("no.such.file")
except IOError, (error, message):
print error, repr(message)
print errno.errorcode[error]
# 2 'No such file or directory'
# ENOENT
|
| I l@ve RuBoard |
|