Python writing binary
binary, file, python, python-3.x
Solution
When you open a file in binary mode, then you are essentially working with the `bytes` type. So when you write to the file, you need to pass a `bytes` object, and when you read from it, you get a `bytes` object. In contrast, when opening the file in text mode, you are working with `str` objects.
So, writing “binary” is really writing a bytes string:
with open(fileName, 'br+') as f:
f.write(b'\x07\x08\x07')
If you have actual integers you want to write as binary, you can use the `bytes` function to convert a sequence of integers into a bytes object:
>>> lst = [7, 8, 7]
>>> bytes(lst)
b'\x07\x08\x07'
Combining this, you can write a sequence of integers as a bytes object into a file opened in binary mode.
As Hyperboreus pointed out in the comments, `bytes` will only accept a sequence of numbers that actually fit in a byte, i.e. numbers between 0 and 255. If you want to store arbitrary (positive) integers in the way they are, without having to bother about knowing their exact size (which is required for struct), then you can easily write a helper function which splits those numbers up into separate bytes:
def splitNumber (num):
lst = []
while num > 0:
lst.append(num & 0xFF)
num >>= 8
return lst[::-1]
bytes(splitNumber(12345678901234567890))
# b'\xabT\xa9\x8c\xeb\x1f\n\xd2'
So if you have a list of numbers, you can easily iterate over them and write each into the file; if you want to extract the numbers individually later you probably want to add something that keeps track of which individual bytes belong to which numbers.
with open(fileName, 'br+') as f:
for number in numbers:
f.write(bytes(splitNumber(number)))
Problem
I use python 3 I tried to write binary to file I use r+b. ``` for bit in binary: fileout.write(bit) ``` where binary is a list that contain numbers. How do I write this to file in binary? The end file have to look like b' x07\x08\x07\ Thanks