Skip to main content

File Handling in Python

Use a context manager (with) so a file is closed even if reading or writing raises an exception. Text files should normally declare an encoding; UTF-8 is a portable default.

Text files and paths​

pathlib.Path provides path operations without manual string concatenation.

from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as directory:
path = Path(directory) / "notes.txt"
path.write_text("alpha\nbeta\n", encoding="utf-8")
lines = path.read_text(encoding="utf-8").splitlines()

print(lines) # ['alpha', 'beta']

Path.read_text() is convenient for small files. Stream large files line by line instead of loading all content into memory.

CSV​

Open CSV files with newline="" as required by the csv module, and specify the encoding separately.

import csv
from io import StringIO

buffer = StringIO(newline="")
writer = csv.DictWriter(buffer, fieldnames=["name", "score"])
writer.writeheader()
writer.writerow({"name": "Ada", "score": 9})
writer.writerow({"name": "Lin", "score": 8})

buffer.seek(0)
rows = list(csv.DictReader(buffer))
print(rows)

CSV values are read as strings unless you convert them. The format does not carry a universal schema, so document delimiter, quoting, missing-value, and type assumptions.

JSON​

json.dump() and json.load() work with file objects; json.dumps() and json.loads() work with strings.

import json

record = {"name": "Ada", "active": True, "scores": [9, 8]}
encoded = json.dumps(record, ensure_ascii=False, sort_keys=True)
decoded = json.loads(encoded)

print(encoded)
print(decoded["scores"]) # [9, 8]

JSON has a smaller type system than Python. Dictionary keys become strings, tuples become arrays, and arbitrary Python objects are not serializable without an explicit conversion.

Binary and spreadsheet files​

Use binary mode ("rb" or "wb") for bytes such as images or compressed data. .xlsx spreadsheets are not handled by the Python standard library; use a maintained library such as openpyxl or pandas and validate its version-specific API in the relevant data-tools lesson.

Never load untrusted pickle data. Unpickling can execute arbitrary code; use a data format designed for interchange when the source is not fully trusted.