Decide when to read a local file as bytes instead of text
Also published in our Blogger archive.
Quick answer
Path.read_bytes preserves raw CRLF bytes, while read_text decodes characters and the default text mode translates newlines.
Example
The fixture asserts the exact byte sequence first, then asserts the text view becomes a\nb\n. Printing both observations makes the selection rule concrete: byte comparisons protect representation, text is for declared character processing.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as d:
p = Path(d) / 'sample.txt'
p.write_bytes(b'a\r\nb\r\n')
raw = p.read_bytes()
text = p.read_text()
assert raw == b'a\r\nb\r\n' and text == 'a\nb\n'
print(f'bytes={len(raw)} text={repr(text)}')
Expected stdout:
bytes=6 text='a\nb\n'
Reading the result
Use an explicit encoding with read_text when the file format requires one. This example uses only ASCII-compatible bytes and does not establish an encoding-detection strategy.
For content validation, decode with an explicitly chosen encoding and report decode failures separately. Bytes are not a substitute for parsing; they answer the narrower question of exact representation.
The reported byte count belongs to the raw representation, while repr of the text makes newline translation visible. The two values should not be compared as if they answered one question.
Sources
- Python pathlib documentation
Prepared with AI assistance. The example uses synthetic data; its stated limits apply.