python 保存bin文件,python bin文件处理
1. 基础知识掌握
bin文件: open函数创建file对象
打开文件的不同模式:例子:open(“file.txt”,‘r’)
模式 描述
r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+ 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
t 文本默认
x 写模式,新建一个文件,如果该文件存在,则会报错。
b 二进制模式
+ 打开一个文件进行更新(可读可写)。
bin文件的python struct库 读写属性
a = struct.pack('B',0x00)
b = struct.unpack(‘B’,a)
这块可以使用python编译的看一下哦!(你就明白了)
1. struct.pack用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,
可以把这里的字符串理解为字节流,或字节数组)。其函数原型为:struct.pack(fmt, v1, v2, ...),
参数fmt是格式字符串,关于格式字符串的相关信息在下面有所介绍。v1, v2, ...表示要转换的python值。
2. struct.unpack做的工作刚好与struct.pack相反,用于将字节流转换成python数据类型。它的
函数原型为:struct.unpack(fmt, string),该函数返回一个元组
Format C Type Python 字节数
x pad byte no value 1
c char string of length 1 1
b signed char integer 1
B unsigned char integer 1
? _Bool bool 1
h short integer 2
H unsigned short integer 2
i int integer 4
I unsigned int integer or long 4
l long integer 4
L unsigned long long 4
q long long long 8
Q unsigned long long long 8
f float float 4
d double float 8
s char[] string 1
p char[] string 1
P void * long
这快就类似于C语言中的字节对齐,大小端的选择
例子:(后面有说明)
a = struct.pack('
b = struct.pack('>I',0x12345678) #四字节存入,按大端存储
Character Byte order Size and alignment
@ native native 凑够4个字节
= native standard 按原字节数
> big-endian standard 按原字节数
! network (= big-endian) standard 按原字节数
2. bin文件的打开和模式
file = open(file_name,'wb')#二进制写模式
file = open(file_name,'rb')#二进制读模式
file = open(file_name,'ab')#二进制补充读写模式
3. bin文件读取
import struct
import os
def ReadFile():
filepath='7.bin'
binfile = open(filepath, 'rb') #打开二进制文件
size = os.path.getsize(filepath) #获得文件大小
for i in range(size):
data = binfile.read(1) #每次输出一个字节
print(data)
binfile.close()
if __name__ == '__main__':
ReadFile()
7.bin文件如下
4. bin文件写入
import struct
import os
def WriteFile():
filepath='7.bin'
data = 123
binfile = open(filepath, 'ab+') #打开二进制文件(追加完成)
a = struct.pack('B',data)
binfile.write(a)
binfile.close()
if __name__ == '__main__':
WriteFile()
7.bin文件
效果图
5. bin文件的大小端写入
import struct
file_name = "5.bin"
def Big_little_endian():
file = open(file_name,'wb')
a = struct.pack('I',0x12345678) #四字节存入,正常状态
b = struct.pack('
c = struct.pack('>I',0x12345678) #四字节存入,按大端存储
file.write(a)
file.write(b)
file.write(c)
file.close()
if __name__ == '__main__':
Big_little_endian()
6. bin文件的写0
import struct
bin_file_1 = "1.bin"
def write0x00():
file = open(bin_file_1,'wb')
for i in range(1,0xFF):
data = struct.pack('B',0x00) #写0
file.write(data)
file.close
if __name__ == '__main__':
write0x00()
效果图:
7. bin文件的写1
import struct
bin_file_2 = "2.bin"
def write0xFF():
file = open(bin_file_2,'wb')
for i in range(0,0xFF):
data = struct.pack('B',0xFF) #写1
file.write(data)
file.close
if __name__ == '__main__':
write0x00()
效果图:无锡正规妇科医院 http://www.jlfukeyy.com/
8. bin文件的合并
1.bin和2.bin合并成merage.bin
import struct
bin_file_1 = "1.bin"
bin_file_2 = "2.bin"
bin_file_merage = "merage.bin"
def merage_bin():
file_merage = open(bin_file_merage,'wb')
file_1 = open(bin_file_1,'rb')
data = file_1.read()
file_merage.write(data)
file_2 = open(bin_file_2,'rb')
data = file_2.read()
file_merage.write(data)
file_1.close()
file_2.close()
file_merage.close()
if __name__ == '__main__':
merage_bin()
效果图:
9. bin文件写入字符串
import struct
import string
bin_file_1 = "7.bin"
enter = memoryview(b"STARTX") #返回值是元组
def writeString():
file = open(bin_file_1,'wb')
for i in enter:
data = struct.pack('B',i)
file.write(data)
file.close
if __name__ == '__main__':
writeString()
效果图:
python 保存bin文件,python bin文件处理相关推荐
- python保存csv_在python中修改和保存csv文件
我有一个csv文件,它的第一行作为列名.在 我正在尝试从旧文件生成一个新的csv文件.在 新的csv文件是通过过滤原始csv文件的第二列(其值为HR)获得的 这是密码import csv import ...
- python保存的是什么类型文件_python-----基本的数据类型和文件操作
1 classstr(basestring):2 """ 3 str(object='') -> string4 5 Return a nice string re ...
- /usr/bin/python^M: 解释器错误: 没有那个文件或目录
[1]问题现象 执行python脚本,提示错误:/usr/bin/python^M: 解释器错误: 没有那个文件或目录 [2]原因分析 大多数是因为脚本文件在windows下编辑过.在windows下 ...
- Linux的Python文件的开头/usr/bin/env python与/usr/bin/python
Linux的Python文件的开头/usr/bin/env python与/usr/bin/python还是有点区别的 开头为#!/usr/bin/env python 例如 dstat命令,当/us ...
- Python-读取文件例子:一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ....
转载自:http://blog.csdn.net/forandever/article/details/5711319 功能: 获取指定目录下面符合一定规则的文件名称和文件修改时间,并保存到指定的文件 ...
- python入门(5)使用文件编辑器编写代码并保存执行
python入门(5)使用文件编辑器编写代码并保存执行 两款文本编辑器: 一个是Sublime Text,免费使用,但是不付费会弹出提示框: 一个是Notepad++,免费使用,有中文界面: 请注意, ...
- 【Python】pandas遍历读取excel文件,修改并保存(一)
code ide and lib version: python 2.7(64) pycharm 2018.1.4 xlrd 1.1. ...
- python保存数据到本地文件_python保存数据到本地文件的方法
python保存数据到本地文件的方法 1.保存列表为.txt文件 #1/list写入txt ipTable = ['158.59.194.213', '18.9.14.13', '58.59.14.2 ...
- 教你用python爬取王者荣耀英雄皮肤图片,并将图片保存在各自英雄的文件夹中。(附源码)
教你用python爬取王者荣耀英雄皮肤图片,并将图片保存在各自英雄的文件夹中.(附源码) 代码展示: 保存在各自的文件夹中 美么? 让我们开始爬虫之路 开发环境 windows 10 python3. ...
- python保存文件夹中的文件路径(绝对路径)
保存文件夹中的文件路径(绝对路径). # !/usr/bin/env python # -*- encoding: utf-8 -*-import osimg_path = '/home/jjuv/D ...
最新文章
- 详解Linux Initrd
- hibernate EJBQL QBC QBE
- 019_Jedis的List数据类型
- C++检查给定数字是否为4的幂的算法实现(附完整源码)
- 【专升本计算机】专升本计算机期末考试复习题(B卷附答案)
- java怎么把system.out的东西输出到文件上
- HTML5与HTML4的比较
- Facebook开源MySQL分支获大佬捧场
- java lib_JavaLib
- debian7 安装配置
- 一个简单漂亮的Java博客系统
- c语言函数库入门,C语言库函数
- 复杂网络matlab程序,复杂网络matlab程序
- java图书管理系统代码_java图书管理系统(源码+jar包+数据库)
- 如何把图纸转换为t3格式_CAD图纸快速转换天正T3格式_鹏业CAD云服务
- 90后的青春,定格在被淡忘的QQ空间里
- 一个高速公路交警的救命忠告
- 「业内分析」拉卡拉新商业模式的转化,拥抱下半场
- 非磺酸型离子液体([pmim]HSO4、[bmim]HSO4、[bpy]HSO4、[ppy]HSO4、[pmim]BF、[pmim]CF3SO3、[pmim]H2PO4)的定制合成
- setup factory打包工具的使用