OSPython Error

OSError

OSError: [Errno 28] No space left on device

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    f.write("test")
OSError: [Errno 28] No space left on device

What causes this error

An operating system call failed. This covers disk errors, network issues, file system problems, and resource exhaustion. The errno attribute indicates the specific OS error.

How to fix it

Catch specific subclasses (FileNotFoundError, PermissionError, etc.) instead of broad OSError. Check `e.errno` for the OS error code. Handle resource cleanup in finally blocks.

Code that causes this error

Broken
# Writing to a full disk
with open("/dev/full", "w") as f:
    f.write("test")

Fixed code

Fixed
try:
    with open("output.txt", "w") as f:
        f.write("test")
except OSError as e:
    print(f"OS error {e.errno}: {e.strerror}")

About OSError

OSError is a broad exception class for operating system-related errors. It serves as the base class for FileNotFoundError, PermissionError, FileExistsError, IsADirectoryError, NotADirectoryError, ConnectionError, TimeoutError, and other system-level exceptions. When you catch OSError, you may receive any of these more specific exceptions.

OSError is raised for disk full conditions, too many open files, broken pipes, network unreachable errors, and other OS-level failures. Each OSError instance has an `errno` attribute containing the numeric error code and a `strerror` attribute with the human-readable message. In modern Python (3.3+), catching the specific subclasses is preferred over catching OSError and checking errno manually, as it leads to cleaner and more readable code.

Common scenarios

1

Running scripts without sufficient file system permissions

2

Connecting to servers that are not running or are unreachable

3

Installing Python packages without proper environment setup

4

Running out of disk space or file descriptors during I/O operations

Related errors