Reading binary file and looping over each byte
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reading a binary file means working with raw bytes rather than decoded text. In Python, the main decisions are whether to iterate one byte at a time, read in chunks for performance, and whether the bytes should later be interpreted as numbers or some other structured format.
Open the File in Binary Mode
Always use binary mode with rb. That prevents Python from trying to decode the file as text or translate newline bytes.
The bytes object is an immutable sequence of integers from 0 to 255. That detail matters because iterating over a bytes object yields integers, not one-character strings.
Loop Over Each Byte
If you truly need to process every byte individually, iterate over the bytes object directly:
This prints decimal byte values such as 137, 80, or 255. That is often enough for checksum logic, simple scanners, or quick debugging of a file header.
If you want a hex view instead of decimal values, format the bytes explicitly:
That pattern is useful when inspecting file signatures. For example, many binary formats start with a recognizable magic number.
Prefer Chunked Reading for Large Files
Reading the entire file at once is fine for small files, but it is wasteful for large inputs. A chunked loop keeps memory usage predictable.
This is the usual production approach. You still examine every byte, but you do not need the entire file in memory at the same time.
Interpret Bytes With struct
Sometimes looping byte by byte is only the first step. Many binary formats store multi-byte integers, floats, or fixed headers. Python's struct module can decode those values once you know the layout.
The format string "<I" means little-endian unsigned 32-bit integer. If the file uses big-endian order, use a format string that starts with ">" instead.
This is how you move from "raw bytes" to meaningful values. The loop gives you access to data at the lowest level, and struct gives you a way to interpret it safely.
Common Pitfalls
One common mistake is opening the file in text mode instead of binary mode. Text mode can change newline handling and tries to decode bytes, which corrupts the meaning of raw binary data.
Another issue is assuming each iterated value is a one-byte bytes object. In Python 3, iterating over bytes yields integers. If you need a one-byte slice, use data[i:i+1].
It is also easy to read an entire multi-gigabyte file into memory just because handle.read() is convenient. For anything large, use chunked reading.
Summary
- Open binary files with
rbso Python returns raw bytes without text decoding. - Iterating over a
bytesobject yields integers from0to255. - Read in chunks for large files so memory usage stays reasonable.
- Use formatting or
enumerate()when you need offsets or hex output. - Use
structwhen byte sequences should be interpreted as typed binary values from a documented format.
Related reading
- Reading contents of a gzip file from a AWS S3 in Python
- Reading streaming http response with Python requests library
- Recalling function Tensor 'object' is not callable
- Received a label value of 1 which is outside the valid range of 0, 1 - Python, Keras
- Recommendations of Python REST web services framework?
- Recursion how to avoid Python set changed set during iteration RuntimeError
- Recursively iterate through all subdirectories using pathlib
- Redirect stdout to a file in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.