Loading...
Persistence stores data beyond one program run. A file format must explain how values are encoded, separated and reconstructed rather than assuming that writing visible text preserves every original type.
What you'll be able to do
A file can contain multiple logical records, each with fields describing an entity. In a text format, separators, quoting and line boundaries need agreed meanings. Splitting every line at a comma fails when a quoted field itself contains a comma; use a format-aware parser for formats such as CSV.
Text files store encoded characters, not abstract Python objects. Reading "42" usually produces text that must be converted if a numerical value is needed. Specify an encoding such as UTF-8 and distinguish an empty field, a missing field and a legitimate zero.
A read operation obtains existing data; writing with a truncating mode can replace existing content, while append adds at the end. A context manager in Python closes the file even when control leaves the block unexpectedly. Check that the path and permissions match the intended task.
A binary file stores bytes interpreted according to a schema or application format. It may compactly represent fixed-width values or media, but byte order, widths and record boundaries must be known. Binary does not mean encrypted, and text is also ultimately represented by bits.
Validate a record’s shape and convert its fields before relying on it. A malformed record should have a defined response rather than silently becoming plausible but incorrect data. File-not-found and invalid-format errors have different causes and may require different recovery paths.
For a fixed-size binary record, an offset can identify its position when the format specifies the record size. Variable-length text records do not generally permit the same simple multiplication. Test both a round trip and failure cases: write known data, read it back and compare values and types.
from io import StringIO, BytesIO
text_file = StringIO("Ada,42\n")
name, score_text = text_file.readline().strip().split(",")
score = int(score_text)
binary_file = BytesIO(bytes([0, 42, 255]))
print(name, score, list(binary_file.read()))Scroll horizontally to see the full code or table.from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory(prefix="kepler-example-") as folder:
text_path = Path(folder) / "scores.txt"
with text_path.open("w", encoding="utf-8") as handle:
handle.write("Ada,42\n")
with text_path.open("r", encoding="utf-8") as handle:
print(handle.read().strip())
binary_path = Path(folder) / "values.bin"
with binary_path.open("wb") as handle:
handle.write(bytes([7, 8]))
with binary_path.open("rb") as handle:
print(list(handle.read()))Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.