The errno Module

The 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


import errno

 fp = open("no.such.file")
except IOError, (error, message):
 if error == errno.ENOENT:
 print "no such file"
 elif error == errno.EPERM:
 print "permission denied"
 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


import errno

 fp = open("no.such.file")
except IOError, (error, message):
 print error, repr(message)
 print errno.errorcode[error]

# 2 'No such file or directory'

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh © 2008-2020.
If you may any questions please contact us: