3 min read
Read whole files with Path.read_text vs open(...).read(). This lab reports reads/s on Linux localhost for a small text file and a ~100 KB medium file — plus a binary read_bytes peer.
Related links:
Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Differentiates from pathlib-vs-ospath (lab 47) — that post compared path joining/stat style APIs; this one is full-file read throughput.
Verdict up front: small (500 B, n=5000): Path.read_text ~97170 reads/s vs open ~97062. Medium (110890 B, n=1000): Path ~17491 vs open ~15493.
| Arm | Pattern |
|---|---|
Path.read_text(encoding=) | pathlib helper |
with open(...) as f: f.read() | classic |
| same on medium file | size sensitivity |
read_bytes / open(...,"rb") | binary peer |
Seven rounds, p50. Texts equal across APIs (equal=True).
Script: lab-evidence/132-path-read-text-vs-open/results/run_lab.py.
| Arm | reads/s |
|---|---|
| Path.read_text small | 97170 |
| open read text small | 97062 |
| Path.read_text med | 17491 |
| open read text med | 15493 |
| Path.read_bytes small | 180811 |
| open rb small | 183833 |
On small text the APIs were essentially tied. On the medium file, Path.read_text edged open. Binary reads roughly doubled throughput vs UTF-8 decode on the small file.
Path.read_text for clarity; do not fear a hidden tax on this box.read_bytes / "rb" and decode only what you need.os.path debates; this post is whole-file read cost.encoding= explicitly for text — locale surprises are worse than microbench noise.Document encoding in the runbook so a “fast open() refactor” does not drop UTF-8 assumptions.
At 110890 bytes, Path landed ~17491 reads/s vs open ~15493. Decode and syscall dominate; the Path wrapper is not the villain. If you repeatedly reread the same config, cache the string — API choice will not save you.
read_bytes / "rb" hit about ~180811 / ~183833 reads/s on the small file — both skip Unicode decode. Use binary when the payload is opaque; do not read_text a protobuf.
For one-shot config loads at process start, either API is fine — the ~97170 reads/s class of results will not be your bottleneck. Standardize on Path in new code so reviews stop bikeshedding open vs Path, and reserve energy for caching, validation, and encoding errors.
Evidence: summary.json, summary.txt, sample files beside the script.
One Linux box, local disk, warm cache. Not NFS, not concurrent readers.
Path.read_text and open().read() tracked closely on small files (~97170 vs ~97062 reads/s); Path led on the medium text file. Prefer Path for readable loaders; use binary reads when you do not need decode.
Lab evidence
Ran lab-evidence/132-path-read-text-vs-open/results/run_lab.py on Linux localhost with Python 3.13.5 on 1 Oct 2026 IST. Seven rounds, p50: small 500 B n=5000 Path.read_text ~97170 reads/s vs open ~97062; medium 110890 B n=1000 Path ~17491 vs open ~15493; binary small read_bytes ~180811 vs open rb ~183833. Text outputs equal=True; warm local cache; affiliates 0.
volta 2.0.2 vs fnm 1.39.0 switching Node 20.19.2 ↔ 22.14.0: session A-then-B median 13.9 ms vs 17.0 ms; steady node -v 7.4 ms vs 6.4 ms (direct 4.7 ms).
Hands-on5 Oct 20265 min read
Summary1 Oct 20263 min read
Summary1 Oct 20263 min read