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. |
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 -
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)
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'
wb 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
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()
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!