问题:
你想读写二进制文件,比如图片,声音文件等等。
解决方案:
使用模式为 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')
在读取二进制数据时,需要指明的是所有返回的数据都是字节字符串格式的,而不 是文本字符串。类似的,在写入的时候,必须保证参数是以字节形式对外暴露数据的对 象 (比如字节字符串,字节数组对象等)。
在读取二进制数据的时候,字节字符串和文本字符串的语义差异可能会导致一个 潜在的陷阱。特别需要注意的是,索引和迭代动作返回的是字节的值而不是字节字符 串。比如:
>>> # Text string >>> t = 'Hello World' >>> t[0]
'H'
>>> for c in t:
... print(c)
...
`H
e
l
l
o
...
>>> # Byte string
>>> b = b'Hello World' >>> b[0]
72
>>> for c in b: ... print(c) ...
72
101 108 108 111 ... >>>
如果你想从二进制模式的文件中读取或写入文本数据,必须确保要进行解码和编 码操作。
with open('somefile.bin', 'rb') as f: data = f.read(16)
text = data.decode('utf-8')
with open('somefile.bin', 'wb') as f: text = 'Hello World' f.write(text.encode('utf-8'))
二进制 I/O 还有一个鲜为人知的特性就是数组和 C 结构体类型能直接被写入,而 不需要中间转换为自己对象。
import array
nums = array.array('i', [1, 2, 3, 4]) with open('data.bin','wb') as f:
f.write(nums)
```