
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I read (or write) binary data in Python?
To read or write a binary file, at first you need to understand the different file modes for Binary Files in Python −
Mode | Description |
---|---|
rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
rb+ | Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file. |
wb | Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
wb+ | Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
ab | Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
ab+ | Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
Read a Binary File
Let’s say we have a binary file. We can read it using the read() method. First, we will open the Binary File using the rb mode of the open() method. We have the following source file in D: drive −
Example
Let’s see the complete code
# Open a binary file f = open('D:\PythonLogo.png', 'rb') # Read lines data = f.read() # Display the data print(data)
Output
The Binary Files are not huma-readable and the content is unrecognizable. The actual output was bigger. We have shown only some part below.
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x04\xb0\x00\x00\x04\xb0\x08\x06\x00\x00\x00 \xeb!\xb3\xcf\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x0 0\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x 93\x00\x00\x00\x07tIME\x07\xe1\x08\n\x07\x16\x07\xc9\xb2\xff\xd3\x00\x00\x80\x00IDATx\xda\ xec\x9dw\xbc]e\x95\xf7\x7f\xcf>\xe7\xa6\x91@\xe8\xc5\x02\xa2\x88\x14q0B\x12\xc0\x99\x00!\x 88(\x02\x9a\x8c\x85"\xea\x80b\xc5\x02\xa1\xf8z\xa78\x8aJ\x13EeF\x1d\xdb\x94\x80@\x08\xea\x a0\x8c\xe0\x0c\x98\x10`\x94\x92\xd0D@,#\x88\xd4\x90r\xef\xd9\xeb\xfd#\x01\x02i\xe7\x9e\xb3 \xcb\xf3\xac\xfd\xfd~>\xef; . . . \xcdn\xd2d\xb1Q\xdf\xfa\xe9\x8d|\x8f\x14\x00\x00\x00\xb4\t\x02\x0b\x00\x00\x00\xe0Q\x98\xa 9\xd0\xd5\xc7\x1c$Izp\xee>\x9a\xd8\xfa\x98\x1d\xffdr\x7f\x153\x8f\xfa\xf8\xa3\xcdQJ\x07\xec\x e5\xc8\xf5X\x95\x96\x1eu\n\xdb\xac\xa4- {\xf9{\xdbe\xf6\xc0#\xfe\xa7\t\xdd\xaf\xed\x9a\xd9\xd1\xf5\xe4\x83\xdawf\xc7w>m\xdb\xbc5- X\xff \xb3\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd02\xff?\xca\x9f\x86X\x17m\x83=\ x00\x00\x00%tEXtdate:create\x002017-08- 10T07:22:07+00:00u\xea\xe6c\x00\x00\x00%tEXtdate:modify\x002017-08- 10T07:22:07+00:00\x04\xb7^\xdf\x00\x00\x00\x00IEND\xaeB`\x82'
Write to a Binary File
The wb mode of the open() method is used to open a file in format form writing.
Note − The Binary Files are not huma-readable and the content is unrecognizable
Let’s see the complete example. Here, the file will get stored in E drive with the name
Example
MyDemoBinary.bin −
# Open a file in binary format for writing f = open("E:\MyDemoBinary.bin","wb") # Elements to be added to the binary file a = [100, 200, 300] # Convert the integer elements to a bytearray myArr = bytearray(a) # The byte representation ius now written to the file f.write(myArr) f.close()
Output
After running the file, the file is visible in the set path i.e. E drive −
- Related Articles
- How do I read a .data file in Python?
- How do I write JSON in Python?
- How do I copy a binary file in Python?
- How to open a binary file in read and write mode with Python?
- How to write binary data to a file using Python?
- How do I test a Python program or component?
- How to read/write data from/to .properties file in Java?
- Write a program in Python to read sample data from an SQL Database
- How do I write class names in Java?
- How do I write method names in Java?
- How do I write variable names in Java?
- How do I write constructor names in Java?
- How do I write interface names in Java?
- How do I write package names in Java?
- How do I write constants names in Java?
