Python 3.5+pathlibfunction
.read_bytes()
reading/writing
Read the whole file into a bytes object — the binary counterpart of `.read_text()`, with no decoding and no encoding parameter.
Syntax
p.read_bytes()
The entire file lands in memory at once, so switch to open(..., 'rb') with chunked reads when the file is large. Unlike .read_text(), nothing is decoded and newlines are left untouched — CRLF stays CRLF — which is exactly why checksums and binary formats go through read_bytes.
Examples
from pathlib import Path
p = Path('test_pathlib.bin')
p.write_bytes(b'\x00\x01')
p.read_bytes() # → b'\x00\x01'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy