使用模式为 ​​rb​​​ 或 ​​wb​​​ 的 ​​open()​​ 函数来读取或写入二进制数据。比如:

# Read the entire file as a single byte string
with open('somefile.bin', 'rb') as f:
data = f.read()

# Write binary data to a file
with open('somefile.bin', 'wb') as f:
f.write(b'Hello World')