How to Read a Binary File in Python

Working on a data-processing project where I had to read and analyze some binary files containing sensor data collected from a manufacturing plant in Texas.

At first, I thought it would be as simple as reading a text file in Python, but I quickly realized that binary files behave differently. They store data in bytes rather than human-readable characters.

In this tutorial, I’ll show you how to read a binary file in Python using different methods. These are the same techniques I use in my professional projects when dealing with images, audio files, or even machine learning model weights.

What is a Binary File in Python?

Before we start writing code, it’s important to understand what a binary file actually is.

A binary file is any file that contains data in a non-text format, meaning that it’s not encoded using plain characters. Examples include images (.jpg, .png), videos (.mp4), PDFs, and executable files (.exe).

Unlike text files, binary files store data as sequences of bytes, which the computer interprets directly. When you open such a file in Python, you need to read it in binary mode to avoid data corruption.

Method 1 – Use open() Function in Binary Mode

The simplest and most common way to read a binary file in Python is by using the built-in open() function with the ‘rb’ mode.

This mode tells Python to open the file in read-binary mode, which means the content will be returned as a sequence of bytes rather than as text.

Here’s how I usually do it in my projects:

# Specify the path to your binary file
file_path = "C:/Users/John/Documents/sensor_data.bin"

# Open the file in binary read mode
with open(file_path, "rb") as binary_file:
    # Read the entire content of the file
    data = binary_file.read()

# Display the first 100 bytes of data
print("First 100 bytes of the binary file:")
print(data[:100])

You can refer to the screenshot below to see the output.

python read binary file

In the code above, I used the with statement to ensure the file is automatically closed after reading. The read() method loads the entire file into memory as a bytes object, which you can then process further.

If you’re dealing with large files, you might want to read them in chunks instead of loading everything at once.

Method 2 – Read a Binary File in Chunks

When working with large binary files, such as images from a satellite or large machine-learning datasets, it’s often inefficient to read the entire file at once.

Instead, I prefer to read the file in chunks, which helps manage memory usage and improves performance.

Here’s how I do that:

file_path = "C:/Users/John/Documents/large_image_file.bin"

# Define the chunk size (for example, 1024 bytes)
chunk_size = 1024

with open(file_path, "rb") as binary_file:
    while chunk := binary_file.read(chunk_size):
        # Process each chunk (for example, print length)
        print(f"Read {len(chunk)} bytes")

You can refer to the screenshot below to see the output.

read binary file python

This approach is especially useful when you need to process binary data on the fly, such as streaming video frames or reading large log files.

Python’s while loop combined with the assignment expression (:=) makes this method both elegant and efficient.

Method 3 – Use readinto() to Read Binary Data into a Buffer

Another efficient way to read binary files in Python is to use the readinto() method.

This method reads bytes directly into a pre-allocated buffer, which can be a bytearray or a memoryview. It’s particularly useful when you want to reuse the same buffer multiple times without creating new objects.

Here’s an example:

file_path = "C:/Users/John/Documents/device_output.bin"

# Create a buffer of 512 bytes
buffer = bytearray(512)

with open(file_path, "rb") as binary_file:
    bytes_read = binary_file.readinto(buffer)

print(f"Number of bytes read: {bytes_read}")
print("First 20 bytes:", buffer[:20])

You can refer to the screenshot below to see the output.

python read file binary

Here, readinto() fills the buffer with data and returns the number of bytes read. This method is faster than repeatedly calling read() because it avoids creating new byte objects each time.

Method 4 – Use the struct Module to Decode Binary Data

When working with binary files, you often need to interpret the bytes as structured data, for example, integers, floats, or strings.

Python’s struct module is perfect for this. It allows you to unpack binary data into readable values according to a specific format.

Here’s an example where I read sensor readings stored as binary integers:

import struct

file_path = "C:/Users/John/Documents/sensor_readings.bin"

with open(file_path, "rb") as binary_file:
    # Read 8 bytes (two integers, each 4 bytes)
    data = binary_file.read(8)

    # Unpack the binary data into two integers
    sensor_1, sensor_2 = struct.unpack('ii', data)

print(f"Sensor 1 Reading: {sensor_1}")
print(f"Sensor 2 Reading: {sensor_2}")

The ‘ii’ format string tells Python to interpret the bytes as two integers. You can use other format codes (like ‘f’ for floats or ‘d’ for doubles) depending on your data structure.

This method is widely used in embedded systems, IoT data parsing, and binary communication protocols.

Method 5 – Read Binary Files Using NumPy

If you’re working with large numerical datasets, the NumPy library provides a faster and more convenient way to handle binary data.

NumPy’s fromfile() function can directly read binary files into an array, which you can then process using vectorized operations.

Here’s how I use it:

import numpy as np

file_path = "C:/Users/John/Documents/temperature_data.bin"

# Read binary data as 32-bit floating-point numbers
data = np.fromfile(file_path, dtype=np.float32)

print("First 10 temperature readings:")
print(data[:10])

This method is incredibly efficient for handling scientific or statistical data. It’s also the preferred way to load binary data for machine learning and data analysis tasks in Python.

Common Use Cases of Reading Binary Files in Python

From my experience, reading binary files in Python is common in many real-world applications. Here are a few examples where I’ve used it professionally:

  • Image Processing: Reading .jpg, .png, or .bmp files as raw bytes before applying filters.
  • Audio Analysis: Loading .wav or .mp3 files for frequency analysis.
  • IoT Devices: Parsing binary data logs from sensors or embedded systems.
  • Machine Learning: Loading pre-trained model weights stored in binary format.
  • Networking: Reading binary packets received from network sockets.

Each of these scenarios requires a slightly different approach, but the core concept, reading bytes and interpreting them correctly, remains the same.

Tips for Working with Binary Files in Python

Over the years, I’ve learned a few best practices that can save you time and headaches:

  1. Always open binary files in ‘rb’ mode.
    Forgetting this can lead to corrupted data or unexpected decoding errors.
  2. Use context managers (with statement) to handle file closing automatically.
    It’s cleaner and prevents resource leaks.
  3. Be careful with file paths.
    Use raw strings (r”C:\path\file.bin”) or forward slashes to avoid escape sequence issues.
  4. Know your file structure.
    If you’re reading structured binary data, always refer to the file’s format specification.
  5. Use libraries wisely.
    For numerical data, NumPy is your best friend. For structured data, use struct. For large files, read in chunks.

So, that’s how you can read binary files in Python using multiple methods, from the simple open() function to more advanced techniques like struct and NumPy.

Each method has its own advantages. If you’re just starting, stick with open() and read(). But if you’re working with large datasets or structured binary data, explore struct and NumPy for better performance and flexibility.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.