python二进制读写及特殊码同步

python对二进制文件的操作需要使用bytes类,直接写入整数是不行的,如果试图使用f.write(123)向文件中以二进制写入123,结果提示参数不是bytes类型。

import os
import struct

a = 0x1A2B3C4D
b = 0x239875ad3d5ffaaa

filepath = 'D:\\wygDocument\\python\\code\\abc.dat'
f_in = open(filepath,'wb+')

for value in range(1,5):
  f_in.write(struct.pack('>I',a))
  f_in.write(struct.pack('>Q',b))
  
f_in.close()
print('Write OK')

猜你喜欢

转载自www.cnblogs.com/mikew/p/11650659.html