You need to read or write binary data in Python, such as that found in images, sound files, and so on. Use the open() function with mode rb or wb to read or write binary data.
For example:
1 2 3 4 5 6 7 |
# Read the entire file as a single byte string with open('filebinary.bin', 'rb') as fl: allBinData = fl.read() # Write binary data to a file with open('filebinary.bin', 'wb') as fl: fl.write(b'Hello World') |
When reading binary, it is important to stress that all data returned will be in the form of byte strings, not text strings. Similarly, when writing, you must supply data in the form of objects that expose data as bytes (e.g., byte strings, bytearray objects, etc.).
When reading binary data, the subtle semantic differences between byte strings and text strings pose a potential gotcha. In particular, be aware that indexing and iteration return integer byte values instead of byte strings.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
>>> # Text string >>> text = "Hello" >>> text[0] 'H' >>> for c in text: ... print(c) ... H e l l o ... >>> # Byte string >>> byteStr = b"Hello" >>> byteStr[0] 72 >>> for c in byteStr: ... print(c) ... 72 101 108 108 111 ... >>> |
If you ever need to read or write text from a binary-mode file, make sure you remember to decode or encode it.
For example:
1 2 3 4 5 6 7 |
with open('mybinfile.bin', 'rb') as fl: data = fl.read(128) text = data.decode('utf-8') # from binary to text with open('mybinfile.bin', 'wb') as fl: text = 'Hello InfinityQuest!' fl.write(text.encode('utf-8')) #from text to binary |
A lesser-known aspect of binary I/O is that objects such as arrays and C structures can be used for writing without any kind of intermediate conversion to a bytes object.
For example:
1 2 3 4 |
import array numbs = array.array('i', [1, 2, 3, 4, 5, 6, 9]) # must be same type with open('myData.bin','wb') as fl: fl.write(numbs) |
This applies to any object that implements the so-called “buffer interface” which directly exposes an underlying memory buffer to operations that can work with it. Writing binary data is one such operation.
Many objects also allow binary data to be directly read into their underlying memory using the readinto() method of files.
For example:
1 2 3 4 5 6 7 8 9 |
>>> import array >>> abc = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> with open('dataBinary.bin', 'rb') as fll: ... fll.readinto(abc) ... 18 >>> abc array('i', [5, 6, 7, 2, 9, 9, 0, 0, 0]) >>> |
However, great care should be taken when using this technique in Python, as it is often platform specific and may depend on such things as the word size and byte ordering (i.e., big endian versus little endian).
We recommend next video tutorial: