【发布时间】:2014-01-04 11:42:16
【问题描述】:
我想将我计算出来的 crc 附加到现有的二进制文件中。
例如,crc 为 0x55667788。
我想在文件末尾追加 0x55、0x66、0x77 和 0x88。
例如,如果我在 HexEdit 中打开文件,文件的最后四个字节 将显示 0x55667788。
到目前为止,这是我的代码:
fileopen = askopenfilename()
filename = open(fileopen, 'rb+')
filedata = filename.read()
filecrc32 = hex(binascii.crc32(filedata))
filename.seek(0,2)
filename.write(filecrc32)
filename.close()
我收到以下错误:
File "C:\Users\cjackel\openfile.py", line 9, in <module>
filename.write(filecrc32)
TypeError: 'str' does not support the buffer interface
有什么建议吗?
【问题讨论】:
-
您正在以 binary 模式打开文件,但正在尝试写入 text 字符串。你需要先为字符串选择一个编码,或者写一些其他的二进制数据。
标签: python file python-3.x append crc