【发布时间】:2013-07-08 21:37:38
【问题描述】:
有没有办法在 python 中写入小于 1 字节的数据 即使我写了以 1 位表示的数字 0,文件大小也是 1(8 位)字节
我尝试了结构模块
file.write(struct.pack('b',0))
数组模块
import array
data1=array.array('B')
x=bin(0)[2:]
data1.append(int(0,2))
f2=open('/root/x.txt','wb')
data1.tofile(f2)
【问题讨论】:
-
没有。一个字节是非空文件的最小大小。
-
我要离开this link to a question about huffman encoding 在这里,因为 OP 在对已接受答案的评论中询问它,这是一个很好的例子,说明如何更好地利用只能写入字节的能力。跨度>