PythonStory_LibraryIO.html
copyright © James Fawcett
Revised: 04/26/2026
9.0 Prologue
Reading and writing data is a core programming task. This chapter covers Python's
built-in file I/O, the with statement for safe resource management,
and the standard modules for structured data formats.
9.1 open() and the with Statement
open(path, mode, encoding) returns a file object. Always use a
with block so the file is closed automatically even if an exception occurs:
# text mode — default, newlines translated, encoding applies
with open("notes.txt", "w", encoding="utf-8") as f:
f.write("line one\n")
f.write("line two\n")
with open("notes.txt", encoding="utf-8") as f:
for line in f: # iterate line by line
print(line.rstrip())
# binary mode — no encoding, no newline translation
with open("data.bin", "rb") as f:
header = f.read(4) # read exactly 4 bytes
Common modes:
"r" (read), "w" (write/create), "a" (append),
"x" (exclusive create, fails if exists), "b" (binary),
"+" (read+write).
9.2 Reading Text Files
Several patterns for reading:
from pathlib import Path
# read entire file as a string
text = Path("notes.txt").read_text(encoding="utf-8")
# read all lines into a list (newlines included)
with open("notes.txt") as f:
lines = f.readlines()
# read lines stripping newlines
with open("notes.txt") as f:
lines = [line.rstrip("\n") for line in f]
# read fixed-size chunks
with open("large.dat") as f:
while chunk := f.read(4096):
process(chunk)
9.3 csv Module
The csv module handles comma-separated values, including quoting and
different delimiters. DictReader maps each row to a dict keyed by
the header row:
import csv
# write
with open("people.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["name", "age"])
writer.writeheader()
writer.writerow({"name": "Alice", "age": 30})
writer.writerow({"name": "Bob", "age": 25})
# read
with open("people.csv", encoding="utf-8") as f:
for row in csv.DictReader(f):
print(row["name"], int(row["age"]))
Use newline="" when opening for writing to prevent double newlines
on Windows. Set delimiter="\t" for TSV files.
9.4 json Module
json serializes Python objects (dicts, lists, str, int, float, bool, None)
to JSON strings and vice versa:
import json
from pathlib import Path
data = {"name": "Alice", "scores": [95, 87, 91], "active": True}
# serialize to string
text = json.dumps(data, indent=2)
# write to file
Path("data.json").write_text(json.dumps(data, indent=2), encoding="utf-8")
# read from file
loaded = json.loads(Path("data.json").read_text(encoding="utf-8"))
# use json.dump / json.load for file objects
with open("out.json", "w") as f:
json.dump(data, f, indent=2)
For types not natively serializable (e.g., datetime, Decimal)
provide a custom default function or subclass json.JSONEncoder.
9.5 pickle Module
pickle serializes arbitrary Python objects to binary data. Unlike JSON it
can round-trip almost any Python object, but the output is Python-specific and should
never be loaded from untrusted sources:
import pickle
obj = {"key": [1, 2, 3], "fn": lambda x: x * 2} # lambdas are NOT picklable
# safe objects
data = {"key": [1, 2, 3], "value": 42.0}
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
with open("data.pkl", "rb") as f:
loaded = pickle.load(f) # ONLY load trusted pickle files
Prefer JSON or other portable formats when interoperability or security matters.
Use pickle for caching Python-specific objects (ML models, numpy arrays) in a
trusted environment.
9.6 io.StringIO and io.BytesIO
In-memory file-like objects are useful for testing and for functions that expect a
file object:
import io, csv
# build a CSV in memory, then parse it
buf = io.StringIO()
writer = csv.writer(buf)
writer.writerows([["a", 1], ["b", 2]])
buf.seek(0)
for row in csv.reader(buf):
print(row)
9.7 Epilogue
This chapter covered text and binary file I/O, the with statement, and the
csv, json, and pickle modules. The next chapter covers the collections, itertools, and
functools modules.
9.8 References
io module — python.org
csv module — python.org
json module — python.org
pickle module — python.org