How to read (or write) binary data in Python?
To read or write binary files, first you need to understand the different file modes for binary files in Python -
model | describe |
---|---|
rb | Open a file in read-only binary format. The file pointer is at the beginning of the file. This is the default mode. |
rb | Open a file in binary format for reading and writing. The file pointer is placed at the beginning of the file. |
wb | Open files written in binary format only. Overwrites the file if it exists. If the file does not exist, a new file is created for writing. |
wb | Open a file in binary format for writing and reading. If the file exists, the existing file is overwritten. If the file does not exist, a new file is created for reading and writing. |
ab | Open a file for appending in binary format. If the file exists, the file pointer is at the end of the file. That is, the file is in append mode. If the file does not exist, a new file is created for writing. |
ab | Open file for appending and reading in binary format. If the file exists, the file pointer is at the end of the file. The file is opened in append mode. If the file does not exist, a new file is created for reading and writing. |
Read binary file
Suppose we have a binary file. We can read it using read() method. First, we will open the binary file using the rb mode of the open() method. We have the following source files in the 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 file is unreadable and the content cannot be recognized. Actual output is greater. Below we show only part of it.
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 binary file
Thewb mode of the open() method is used to open the file in writing format.
Note - The binary file is not readable and the content cannot be recognized
Let's take a look at the complete example. Here, the file will be 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 will be visible in the set path, that is, the E drive -
The above is the detailed content of How to read (or write) binary data in Python?. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools