OSError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
OSError means your Python code tried to do something with the operating system — like open a file, connect to a network, or run a process — and the OS refused or failed. It is a broad category of errors that covers file system problems, permission issues, and hardware failures. Python raises OSError whenever the underlying OS operation returns an error.
Affected Models
- Python 3.3 and later
- All platforms (Windows, Linux, macOS)
Common Causes
- Trying to open a file that does not exist at the path you specified
- Not having permission to read, write, or execute a file or directory
- The disk is full and Python cannot write any more data
- Trying to use a network socket or port that is already in use or unavailable
- Attempting to delete or rename a file that is locked by another process
How to Fix It
-
Read the full error message. Python includes the specific reason — such as 'No such file or directory', 'Permission denied', or 'No space left on device'. This tells you exactly what went wrong.
The errno attribute on the exception also gives you a numeric code. For example, errno 2 = file not found, errno 13 = permission denied.
-
For file not found errors, double-check the file path. Print the path variable before using it to make sure it is what you expect.
On Windows, use raw strings (r'C:\path\to\file') or forward slashes to avoid backslash escape issues.
-
For permission denied errors, check whether your script is running with sufficient rights. On Linux/macOS you may need to run with sudo, or change the file permissions using chmod.
Never run your entire script as root just to fix a permission error. Fix the permissions on the specific file instead.
-
For disk full errors, free up disk space or redirect output to a different drive. Check available space with shutil.disk_usage('/').
If writing a large file, check available space before starting to write to avoid a partial write that corrupts the file.
-
Wrap OS operations in a try/except block to handle OSError gracefully. Log the error, inform the user, or fall back to an alternative path.
Example: try: f = open(path) except OSError as e: print(f'Could not open file: {e}')
When to Call a Professional
Most OSErrors are fixable by checking file paths, permissions, or disk space. If the error relates to network hardware, corrupted storage, or system-level permissions that you cannot change, a system administrator may need to help.
Frequently Asked Questions
What is the difference between OSError and IOError?
In Python 3, IOError is just an alias for OSError — they are exactly the same thing. In Python 2 they were different, but since Python 3.3, all OS and IO errors were merged under OSError. You may still see IOError in older code, but it behaves identically to OSError.
Why does OSError sometimes have a sub-class like FileNotFoundError?
Python 3 introduced specific sub-classes of OSError for common cases: FileNotFoundError, PermissionError, TimeoutError, ConnectionError, and others. These are all OSError — they just give you a more specific name to catch. You can catch OSError to handle all of them at once, or catch the specific sub-class for finer control.
How do I check what kind of OSError I got?
Use the errno attribute: import errno, then check e.errno == errno.ENOENT (file not found) or e.errno == errno.EACCES (permission denied). Or just check the type: isinstance(e, FileNotFoundError). Printing the exception itself also shows a human-readable description.