IOError
IOError: [Errno 5] Input/output error
Traceback
Traceback (most recent call last):
File "main.py", line 4, in <module>
print("caught")
IOError: [Errno 5] Input/output errorWhat causes this error
An I/O operation failed at the system level. IOError is a legacy alias for OSError, maintained for backward compatibility.
How to fix it
Use OSError or specific subclasses (FileNotFoundError, PermissionError) in new code. IOError can be directly replaced with OSError. Catch the most specific exception that applies to your situation.
Code that causes this error
try:
f = open("missing.txt")
except IOError:
print("caught")Fixed code
try:
f = open("missing.txt")
except FileNotFoundError:
print("File not found")
except OSError as e:
print(f"I/O error: {e}")About IOError
IOError is a legacy alias for OSError. In Python 3.3+, IOError was merged into OSError — they are the same class. Code that catches IOError still works, but new code should use OSError or its specific subclasses.
The original purpose of IOError was to represent I/O-related failures: reading from or writing to files, network I/O failures, device errors, and similar system-level I/O problems. With the Python 3.3 exception hierarchy reorganization, these are now covered by OSError subclasses like FileNotFoundError, PermissionError, IsADirectoryError, and BrokenPipeError. The merge simplified the exception hierarchy and made it more Pythonic — instead of catching a broad exception and checking errno, you catch the specific exception type you care about.
Common scenarios
Opening files with incorrect or relative paths
Reading files that have been moved, renamed, or deleted
Writing to directories instead of files or vice versa
Working with file streams that have been closed prematurely