Python read binary file and iterate over each byte
Codemia Knowledge Hub ·
To loop over each byte of a binary file in Python, open it in rb mode and iterate over the bytes object you read. Each iteration yields an integer from 0 to 255, not a one-character string. For large files, read in chunks so memory use stays predictable.
Open the file in binary mode
Always use rb. Text mode tries to decode the file and can change newline handling, which corrupts raw binary data.
bytes is an immutable sequence of integers from 0 to 255, which is why iterating over it yields integers.
Loop over each byte
This prints decimal values such as 137, 80 or 255, which is often enough for checksum logic, simple scanners or quick debugging of a file header. If you need a one-byte bytes object instead of an integer, slice: data[i:i+1].
For a hex view with offsets, use enumerate() and format the values:
This helps when inspecting file signatures, since many binary formats start with a recognizable magic number.
Read large files in chunks
handle.read() loads the whole file, which is fine for small files but wasteful for multi-gigabyte ones. A chunked loop still visits every byte without holding the entire file in memory:
This is the usual production approach.
Interpret bytes with struct
Many binary formats store multi-byte integers, floats or fixed headers. Once you know the layout, the struct module decodes them:
"<I" means a little-endian unsigned 32-bit integer. For big-endian data, start the format string with ">" instead. The byte loop gives you the lowest-level access, and struct turns those bytes into meaningful values.
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?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseThere is a whole course on this
Basis Lab has written “Bytes, Text and File Formats”, a course that teaches this from the ground up rather than answering the single question. It is a separate product from the same team and the course is free to read.
Read the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.