Source: 📖 Effective Python item 3
Writing binary data to a file requires opening the file with writing binary wb
mode:
with open('file.bin', 'wb') as f:
f.write(...)
Reading binary data from a file requires opening the file with read binary rb
mode:
with open('file.bin', 'rb') as f:
data = f.read()
See also: