File I/O & File Systems
5. Operating System File Descriptors, Storage & I/O Subsystems
5.1OS File Descriptors & Kernel Handles
When your program wants to read or write a file stored on a hard drive or SSD, Python cannot access the physical disk directly. Instead, Python asks your computer's Operating System (OS) for permission. The OS opens the file and hands Python an integer reference code called a File Descriptor (or file handle).
ANALOGY TIME
If a program opens thousands of files without closing them, it consumes all available file descriptors, causing a system crash known as Resource Exhaustion (File Descriptor Leak)!
5.2The Context Manager Protocol (with statement)
To prevent resource leaks, Python provides the Context Manager (with statement). A context manager guarantees that a file is automatically and safely closed the moment your code exits the with block—even if an unexpected crash or error occurs inside!
Under the hood, any object used in a with statement must implement two special methods:
__enter__(): Opens the resource and returns the file object.__exit__(): Cleans up and closes the file handle safely.
5.3File Modes & Line Streaming (Preventing Out-of-Memory Crashes)
When opening files with open(), Python supports different modes: 'r' (read text), 'w' (write/overwrite text), 'a' (append to end), and 'b' (binary mode for images/audio).
- The Out-of-Memory (OOM) Danger: Calling
file.read()loads the ENTIRE file into RAM all at once. If you try to open a 10 Gigabyte log file on a laptop with 8 Gigabytes of RAM, your computer will freeze and crash! - Line Streaming: Instead of reading the whole file at once, Python files are iterable. Using a
for line in file:loop reads the file one line at a time from disk, using virtually zero RAM! - Analogy: Like drinking water from a straw one sip at a time versus trying to swallow an entire bucket of water in one giant gulp!
5.4Cross-Platform Paths (pathlib.Path) & Atomic Writing
Different operating systems write file directory paths differently: Windows uses backslashes (C:olderile.txt), while Linux and Mac use forward slashes (/folder/file.txt).
- `pathlib.Path`: Python's modern built-in module for working with file paths cleanly across Windows, Mac, and Linux without writing custom slash code.
- Atomic Writing: Writing data to a temporary file first and swapping it into place in one instant step so that a power failure never leaves you with a half-written corrupt file.
5.5Code Demo 1: Safe File I/O with Context Managers
Here is the recommended, bulletproof way to read and write files in Python:
5.6Code Demo 2: Streaming Line Filter (OOM Prevention)
Filter huge log files while keeping memory consumption tiny:
GOTCHA 1
Opening files using f = open(...) without a with context manager, leading to leaked file descriptors if an exception is raised before f.close().
GOTCHA 2
Opening a file in write mode 'w' assuming it will append. 'w' instantly truncates (erases) all existing file contents! Use 'a' to append.
GOTCHA 3
Forgetting to specify explicit encoding='utf-8'. Platform defaults (e.g. Windows CP1252) can cause UnicodeDecodeError when processing UTF-8 text.
GOTCHA 4
Reading huge log files into memory using f.read() or f.readlines(), which can exhaust RAM on large production files. Iterate over f line-by-line instead.
WHERE THIS SHOWS UP IN REAL CODE
Applications continuously interact with file systems—reading configuration files (.env, .json), streaming web server access logs, generating CSV export reports, and persisting data buffers.
TRY THIS YOURSELF
Modify Demo 2 to filter and print lines that contain 'WARN' in addition to 'ERROR'.
Notice how pathlib and open work seamlessly together.
Demonstrate file creation, context managers, line streaming, and pathlib utilities.
Let's build an audit log filter that counts failed HTTP requests!
Steps to Complete:
- Write 4 lines to
access.log: `200 OK
404 NOT_FOUND 200 OK 500 SERVER_ERROR`.
- Read
access.logand count lines that do not start with200. - Print
Failed Requests: 2.
Expected Output:
Why is it best practice to open files using the `with open(...)` statement?
What happens when you open an existing file in write mode `'w'`?
Why should line-by-line iteration (`for line in f:`) be used when processing multi-gigabyte files?
Which module provides object-oriented, cross-platform file path manipulation in Python?
What is the purpose of specifying `encoding='utf-8'` when opening text files?
Which file open mode allows you to append data to an existing file without deleting prior text?
# comments · files
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.