Read Binary File in Python
Read bytes from a binary file and print their length.
BeginnerFile Handling ProgramsExample 10 of 20
read-binary-file.py
Run in browser1# Program to read a binary file23filename = input("Enter binary filename: ")45try:6 with open(filename, "rb") as f:7 data = f.read()8 print("Read", len(data), "bytes")9except FileNotFoundError:10 print("Binary file not found.")
Output
Enter binary filename: image.png Read 10240 bytes
What's going on
Opening in "rb" mode reads raw bytes rather than decoding text.