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')
 1 import os
2 import struct
3
4 #Read file into list
5 with open('E:/fep/tj19.txt','r') as f:
6 line = f.read().strip()
7 linestr = line.split('\n') #splited by line breaks
8 #linestr = line.split(r"[\s\n]",line) #splited by Space character and line breaks
9
10 f_out = open('E:/fep/realData.dat','xb')
11
12 frame_flag_0 = 0x1A2B3C4D5E6F
13 mask = 0xFFFFFFFFFFFF
14
15 for i in range(0,len(linestr)):
16 filepath = 'E:/fep/19/' + linestr[i]
17 f_in = open(filepath,'rb')
18 f_size = os.path.getsize(filepath)
19 f_in.seek(109)
20
21 offset = 109
22 chunk = 905
23
24 while True:
25 if offset >= f_size:
26 break
27 buff = f_in.read(15)
28 offset += chunk
29
30 unpacked_data = struct.unpack_from('>ih',buff,9) #start from the 9th byte
31 if(unpacked_data[0] != 439041101) or (unpacked_data[1] != 24175) #1A2B3C4D 5E6F
32 f_out.write(buff)
33 f_out.write(f_in.read(890)) # 9 + 6 + 890
34
35 f_in.seek(offset)
36
37 f_in.close()
38 f_out.flush()
39
40 f_out.close()
41 print('OK')

最新文章

  1. System.getProperty()引起的悲剧--您的主机中的软件中止了一个已建立的连接
  2. PostMessager来对子父窗体进行跨域
  3. Linux之Sed命令详解(总结一些实用例子)
  4. LoadRunner中响应时间与事物时间详解
  5. Verilog实现IIC通讯第二版
  6. Struts2语法--Ognl
  7. php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明
  8. 如何使用phpstudy本地搭建多站点(每个站点对应不同的端口)
  9. LeetCode 226. Invert Binary Tree (反转二叉树)
  10. node的应用场景
  11. windows下解压zip包,包含中文解析
  12. (NGUI)UISprite 切换图集
  13. excel文字格式设置
  14. Hive时间函数笔记
  15. Linux 系统使用 iso 镜像文件或光盘配置本地YUM 源的最简单方式
  16. 大坑记录 - shell脚本删除操作
  17. mysql 中显示 table 的基本信息
  18. C#创建word,操作、读写
  19. 由于没有发现潜在的递归导致MySQL链接数溢出:MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connec
  20. select支持多查询,获取分页count

热门文章

  1. python基础之内建模块base64
  2. shell应用之习题一
  3. HTML html5 语义化标签
  4. SpringMVC Web 相对路径与绝对路径
  5. ThinkPHP无限级分类(递归)
  6. Nacos源码结构和AP模式注册中心实现介绍
  7. Go语言协程并发---原子操作
  8. Python+Selenium+Appium+API学习使用过的命令
  9. 永远的ace 实验七 团队作业4—团队项目需求建模与系统设计(1)
  10. npm基本用法及原理(10000+)