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

Posted mikewy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 

以上是关于python二进制读写及特殊码同步的主要内容,如果未能解决你的问题,请参考以下文章