CoursesProgramsCompaniesQuizPrepAbout
Programs/Python/File Handling Programs

10 / 20

Next
1. Read File2. Write File3. Append to File4. Read CSV File5. Write CSV File6. JSON Read7. JSON Write8. File Copy9. File Rename10. Read Binary File11. Write Binary File12. Count Words in File13. Count Lines in File14. Count Characters in File15. Search Substring in File16. Iterate File Lines17. File Metadata18. File Compression19. File Decompression20. Delete File

File Handling Programs

  1. 01Read File
  2. 02Write File
  3. 03Append to File
  4. 04Read CSV File
  5. 05Write CSV File
  6. 06JSON Read
  7. 07JSON Write
  8. 08File Copy
  9. 09File Rename
  10. 10Read Binary File
  11. 11Write Binary File
  12. 12Count Words in File
  13. 13Count Lines in File
  14. 14Count Characters in File
  15. 15Search Substring in File
  16. 16Iterate File Lines
  17. 17File Metadata
  18. 18File Compression
  19. 19File Decompression
  20. 20Delete File

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 browser
1# Program to read a binary file
2
3filename = input("Enter binary filename: ")
4
5try:
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.

PreviousFile RenameNextWrite Binary File

On this page

CodeOutputExplanation
Up nextWrite Binary File
Schoolabe

Learn, Practice, Ace Coding

ProgramsCompaniesTutorialsQuizInterview PrepCareer QuestGATE CS prepGATE CS 2027 study planJob Ready
AboutContactCareersBlog
DocumentationAPI ReferenceCommunitySupport
Privacy PolicyTermsRefund PolicyCookie Policy

© 2026 Schoolabe