File I/O in Python
Read and write files safely with context managers, explicit encodings, and line-oriented streaming.
Quick read/write
The built-in open returns a file object. Always specify the mode and encoding for text.
text = Path("notes.txt").read_text(encoding="utf-8")
Path("out.txt").write_text("hello\n", encoding="utf-8")
Or with open:
with open("data.txt", "w", encoding="utf-8", newline="") as f:
f.write("hello\n")
Modes and encodings
Common modes: "r" read, "w" write (truncate), "a" append, "x" create, "b" binary, "t" text (default). Always set encoding for text files.
with open("data.bin", "wb") as f:
f.write(b"\x00\x01")
Streaming line by line
Iterate files to avoid loading everything into memory.
with open("big.csv", encoding="utf-8") as f:
for line in f:
cols = line.rstrip("\n").split(",")
newline="" preserves newlines exactly (important for csv module on Windows).
Pathlib (recommended)
Use pathlib.Path for portable path operations.
from pathlib import Path
p = Path("/tmp") / "file.txt"
if p.exists():
data = p.read_bytes()
JSON and CSV
Use stdlib helpers for structured text.
import json, csv
obj = json.loads('{"a":1}')
with open("data.json", "w", encoding="utf-8") as f:
json.dump(obj, f, ensure_ascii=False, indent=2)
with open("rows.csv", "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["a", 1])
Temporary files and directories
Use tempfile to create safe temporary files.
import tempfile
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmp:
tmp.write("data")
name = tmp.name
Errors and cleanup
with closes files even if an exception occurs. Handle FileNotFoundError, PermissionError, etc., where appropriate.
Best practices
- Always use context managers (
with) - Specify
encodingand, for CSV,newline="" - Stream large files; avoid
read()on huge inputs - Prefer
pathlibAPIs for paths
Summary
- Use
open/withsafely; be explicit with modes and encodings - Stream lines for scale; leverage stdlib (
json,csv,pathlib,tempfile)