Course contentsShow
← Python
Lesson 391 of 1,0606. Error Handling and File I/OFree lesson

Reading and Writing Binary Data Files

Perform low-level file operations to handle raw binary data, essential for processing images, audio files, or custom packed data formats.

In this lesson, you will learn how to manipulate raw bytes directly in Python to read and write files that aren't stored as plain text.

Understanding Binary vs. Text

When you open a file in text mode (default mode='r'), Python interprets bytes based on an encoding like UTF-8. When handling images, audio, or custom packed binary formats, you must use binary mode by appending b to your mode string (e.g., 'rb' for reading, 'wb' for writing). This tells Python to skip the text encoding and provide you with the raw bytes objects.

Working with Raw Bytes

A bytes object is a sequence of integers (0-255). Because binary data lacks the structure of lines or paragraphs, you typically read them in fixed-size chunks using the .read(n) method, where n is the number of bytes to retrieve.

Here is how you might copy a small binary file:

# Open the source for reading and the destination for writing
try:
    with open('data.bin', 'rb') as source:
        with open('backup.bin', 'wb') as destination:
            # Read 1024 bytes at a time
            chunk = source.read(1024)
            while chunk:
                destination.write(chunk)
                chunk = source.read(1024)
except IOError as e:
    print(f"An error occurred: {e}")

Why Use Binary Mode?

If you try to read a binary file (like a JPEG image or a compiled executable) in text mode, Python will try to decode it as UTF-8. This will likely trigger a UnicodeDecodeError because many byte sequences in binary files are invalid in text encoding. Binary mode treats the data as a "black box" of bytes, ensuring that every byte you read from the source is identical to the one written to your destination.

By combining this with the context manager pattern (with statements), you ensure that your file handles are closed properly, preventing memory leaks or file corruption even if an exception occurs during the read/write process.

Key Takeaway: Always use binary mode ('rb' or 'wb') when working with non-text files to prevent encoding errors and ensure the integrity of your raw data.