bytearray
A changeable sequence of bytes, often used as a buffer for binary input and output.
In more detail
Unlike bytes, a bytearray can be changed by index, with values from 0 to 255. A binary handle's readinto(buf) fills an existing bytearray and returns how many bytes it filled, so one buffer can be reused for every chunk. When fewer bytes remain than the buffer holds, only its front is overwritten.
Example
buf = bytearray(4)
with open('data.bin', 'rb') as f:
n = f.readinto(buf)Related terms
- bytes — An immutable sequence of small integers from 0 to 255, the raw form of encoded text and binary data.
- binary mode — Opening a file with b in its mode, so reads return bytes exactly as stored.
- mutable — Able to be changed in place after it is created, like most lists, arrays and objects in languages such as Python and JavaScript.
Taught in
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep