Standard errno system symbols in Python

Python provides built-in error handling through the errno module, which contains standard system error codes and messages. These error codes help identify specific types of errors that can occur during program execution.

Listing All Standard Error Codes

The errno module contains predefined error numbers and their corresponding symbolic names. We can list all available error codes using the errorcode dictionary along with os.strerror() to get human-readable descriptions ?

import errno
import os

print("Error Code : Description")
print("-" * 30)

for error_code in sorted(errno.errorcode):
    print(f"{error_code} : {os.strerror(error_code)}")
Error Code : Description
------------------------------
1 : Operation not permitted
2 : No such file or directory
3 : No such process
4 : Interrupted system call
5 : Input/output error
6 : No such device or address
7 : Argument list too long
8 : Exec format error
9 : Bad file descriptor
10 : No child processes
...

Using Error Codes in Exception Handling

Error codes are particularly useful when handling specific types of exceptions. Here's an example that demonstrates how to catch and handle different file-related errors ?

import errno

try:
    with open('nonexistent_file.txt', 'r') as file:
        content = file.read()
        
except IOError as e:
    if e.errno == errno.ENOENT:  # Error code 2: No such file or directory
        print(f"Error: {e.strerror}")
        print("The file you're trying to access does not exist.")
        
    elif e.errno == errno.EACCES:  # Error code 13: Permission denied
        print(f"Error: {e.strerror}")
        print("You don't have permission to access this file.")
        
    elif e.errno == errno.EISDIR:  # Error code 21: Is a directory
        print(f"Error: {e.strerror}")
        print("You're trying to open a directory as a file.")
        
    else:
        print(f"Unexpected error occurred: {e.strerror}")
Error: No such file or directory
The file you're trying to access does not exist.

Common Error Codes

Error Code Symbolic Name Description
2 ENOENT No such file or directory
13 EACCES Permission denied
17 EEXIST File exists
21 EISDIR Is a directory

Checking Specific Error Conditions

You can also use symbolic names from the errno module instead of numeric codes for better code readability ?

import errno
import os

def create_file(filename):
    try:
        # Try to create a new file (fails if file already exists)
        fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
        os.close(fd)
        print(f"File '{filename}' created successfully.")
        
    except OSError as e:
        if e.errno == errno.EEXIST:
            print(f"Error: File '{filename}' already exists.")
        else:
            print(f"Error creating file: {e.strerror}")

# Test the function
create_file("test.txt")
create_file("test.txt")  # This will fail since file already exists
File 'test.txt' created successfully.
Error: File 'test.txt' already exists.

Conclusion

The errno module provides a standardized way to handle system errors in Python. Using symbolic names like errno.ENOENT makes code more readable and portable than using numeric error codes directly.

Updated on: 2026-03-15T18:11:42+05:30

571 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements