python
binary-files
file-io
looping
duplicate-content

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.

Browse interview questions

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.

python
1with open("sample.bin", "rb") as handle:
2    data = handle.read()
3
4print(type(data))   # <class 'bytes'>
5print(len(data))

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:

python
with open("sample.bin", "rb") as handle:
    for byte_value in handle.read():
        print(byte_value)

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:

python
with open("sample.bin", "rb") as handle:
    for offset, byte_value in enumerate(handle.read()):
        print(f"{offset:04x}: {byte_value:02x}")

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.

python
1chunk_size = 4096
2
3with open("sample.bin", "rb") as handle:
4    while True:
5        chunk = handle.read(chunk_size)
6        if not chunk:
7            break
8
9        for byte_value in chunk:
10            pass  # process each byte here

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.

python
1import struct
2
3with open("numbers.bin", "rb") as handle:
4    header = handle.read(4)
5    value = struct.unpack("<I", header)[0]
6    print(value)

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 rb so Python returns raw bytes without text decoding.
  • Iterating over a bytes object yields integers from 0 to 255.
  • Read in chunks for large files so memory usage stays reasonable.
  • Use formatting or enumerate() when you need offsets or hex output.
  • Use struct when byte sequences should be interpreted as typed binary values from a documented format.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.