Python File Handling Cheat Sheet
Reading and writing files in Python covering open modes, line iteration, pathlib path operations, and CSV/JSON serialization.
Opening & Modes
The with statement guarantees the file is closed.
with open("data.txt", "r") as f: # 'r' read (default) contents = f.read()# file is automatically closed here, even on exception# common modes:# 'r' read (default) 'w' write, truncates existing file# 'a' append 'x' create, fails if file exists# 'rb' read binary 'r+' read and write
Reading Files
Three common ways to read file contents.
with open("data.txt") as f: contents = f.read() # entire file as one stringwith open("data.txt") as f: lines = f.readlines() # list of lines, keeps '\n'with open("data.txt") as f: for line in f: # memory-efficient, one line at a time print(line.strip())
Writing & Appending
Creating and adding to files.
with open("out.txt", "w") as f: f.write("Hello\n") f.writelines(["line1\n", "line2\n"])with open("out.txt", "a") as f: f.write("appended line\n") # adds without erasing existing content
pathlib for Path Operations
The modern, object-oriented way to work with file paths.
from pathlib import Pathp = Path("data") / "file.txt" # cross-platform path joiningp.exists()p.is_file()p.read_text()p.write_text("content")list(Path(".").glob("*.py")) # all .py files in current dir
CSV & JSON
Reading and writing structured data formats.
import csvwith open("data.csv", newline="") as f: reader = csv.DictReader(f) for row in reader: print(row)import jsonwith open("data.json") as f: data = json.load(f)with open("out.json", "w") as f: json.dump(data, f, indent=2)
Atomic Writes with tempfile + os.replace
Prevents readers from ever seeing a half-written file if the process crashes mid-write.
import tempfile, osdef atomic_write(path, data): dir_name = os.path.dirname(path) or "." fd, tmp_path = tempfile.mkstemp(dir=dir_name) try: with os.fdopen(fd, "w") as f: f.write(data) f.flush() os.fsync(f.fileno()) # force to disk before rename os.replace(tmp_path, path) # atomic on POSIX and Windows except Exception: os.unlink(tmp_path) raise
Memory-Mapped Files with mmap
Random access into large files without loading them entirely into RAM.
import mmapwith open("big.bin", "r+b") as f: with mmap.mmap(f.fileno(), 0) as mm: # 0 = map the whole file print(mm[0:16]) # slice like bytes mm[0:4] = b"HEAD" # write in place, flushed on close idx = mm.find(b"MAGIC") # fast search without a full read mm.flush()
In-Memory Streams: StringIO / BytesIO
Treating strings or bytes as file-like objects, useful for testing and streaming APIs.
from io import StringIO, BytesIOimport csvbuf = StringIO()writer = csv.writer(buf)writer.writerow(["name", "score"])writer.writerow(["Ada", 95])csv_text = buf.getvalue() # no disk I/O neededbin_buf = BytesIO(b"\x00\x01\x02")bin_buf.seek(0)chunk = bin_buf.read(2)
Packing/Unpacking Binary Data with struct
Reading and writing fixed-layout binary records, e.g. custom file formats or network protocols.
import struct# format: big-endian, unsigned int (4B), float (4B), 8-byte stringfmt = ">If8s"record = struct.pack(fmt, 42, 3.14, b"label\x00\x00\x00")with open("records.bin", "wb") as f: f.write(record)with open("records.bin", "rb") as f: data = f.read(struct.calcsize(fmt)) num, flt, label = struct.unpack(fmt, data)
Advanced os / pathlib Utilities
Less common but frequently useful filesystem functions.
- os.scandir()- like os.listdir() but returns DirEntry objects with cached stat info, much faster for large directories
- Path.resolve()- returns the absolute path with symlinks resolved and '..'/'.' collapsed
- Path.with_suffix() / with_name()- return a new Path with the extension or filename swapped out, without string manipulation
- shutil.copy2() / shutil.move()- copy2 preserves metadata (timestamps), move() falls back to copy+delete across filesystems
- os.fspath()- normalizes Path-like or str-like objects to a plain string, used internally by APIs accepting PathLike
- tempfile.TemporaryDirectory()- creates a self-cleaning temp directory as a context manager
- Path.stat().st_mtime- last-modified timestamp, common for cache invalidation without external libraries
Always specify `encoding="utf-8"` explicitly when opening text files — the default encoding is platform-dependent, so code that works on your machine can silently misread files on a server with a different locale.