binary mode
Opening a file with b in its mode, so reads return bytes exactly as stored.
In more detail
In binary mode nothing is decoded or converted: read() returns a bytes object and write() requires bytes. A binary handle can also fill an existing bytearray with readinto(). Use it for images, archives and any data that is not text.
Example
with open('data.bin', 'rb') as f:
raw = f.read() # bytesRelated terms
- text mode — The default way of opening a file, which decodes bytes into str using an encoding.
- bytes — An immutable sequence of small integers from 0 to 255, the raw form of encoded text and binary data.
- bytearray — A changeable sequence of bytes, often used as a buffer for binary input and output.
Taught in
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep