Read Binary File

Read bytes from a binary file and print their length.

BeginnerTopic: File Handling Programs
Back

Python Read Binary File Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to read a binary file

filename = input("Enter binary filename: ")

try:
    with open(filename, "rb") as f:
        data = f.read()
        print("Read", len(data), "bytes")
except FileNotFoundError:
    print("Binary file not found.")
Output
Enter binary filename: image.png
Read 10240 bytes

Understanding Read Binary File

Opening in "rb" mode reads raw bytes rather than decoding text.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents