Python

Python Errno

Computing languages have a system for managing errors. Some languages have pre-programmed error codes. Python has connections to common error codes. When a function is called in Python, the global parameter “errno” is given a signal to determine the type of error. This is a public variable declared in the headers file errno that shows if an error happened during a procedure call.

This article will show how to find built-in error codes and error numbers via the below contents:

What is an “errno” Module in Python?

The Python “errno” module helps to manage errors. It matches error codes with their names, which helps to understand error messages easily. It also has codes for different platforms and abstract identities to manage errors across platforms. The errno module declared several symbolic error codes, such as EPERM (“permission denied”) and ENOENT (“no such file or directory”).

Let’s understand the “errno” module in detail using several examples:

Example 1: Listing out the Inbuilt Error Numbers and Error Codes

To show the functionality of the “errno” module, we can use the “os” library in conjunction. By importing these libraries and utilizing the “sorted()” function, we can retrieve and display a list of built-in error codes along with their descriptions:

import errno
import os
for error_code in sorted(errno.errorcode):
    print(error_code, ':', os.strerror(error_code))

All the error codes, along with their numbers, have been listed in the console output:

The output continues to show more errors that occur for the Python errno module:

Example 2: Handling Absent Files with “try” and “except”

Python can manage errors beyond standard codes. We can use try and except statements to manage specific exceptions. For instance, if the file opens and does not present/exist, we can catch the “IOError” error/exception and give feedback to users. The “e.errno” attribute contains the error code, and the “e.strerror” attribute contains the error message. In this case, the error code is “2”, which corresponds to the error message “No such file or directory”:

try:
    file = open('NonExistentFile.txt')
except IOError as e:
    if e.errno == 2:
        print(e.strerror)
        print("The File Doesn't Exist!")

This code retrieves the following output:

Example 3: Get Error Code and Symbolic Name

The below code will print the string “ENOENT”, which is the symbolic name for the error code “ENOENT” (no such file or directory). We can also print the integer “2”, which is the error code for the symbolic name ENOENT:

import errno
print('Symbolic Name: ', errno.errorcode[errno.ENOENT])
print('Error Code for a Symbolic Name: ', errno.ENOENT)

The above code displays the following output:

Note: You can check this official documentation for all available symbolic names.

Conclusion

The “errno” module in Python defines a dictionary called “errorcode” that maps from error codes to symbolic names. For example, the error code 1 (which means “Operation not permitted”) is mapped to the symbolic name errno.EPERM. This guide delivered a comprehensive tutorial on the Python “errno” module using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.