python bin文件處理

在下這廂有禮了

0. 軟件準備

python 版本 Python 3.7.1
大概花費時間:3天半抽空學習的知識分享

bin文件查看器:https://pan.baidu.com/s/1_XebNTlh052zt5Xa58xDMQ 提取碼:f4uj

生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('<I',0x12345678)  #四字節存入,按小端存儲
b = struct.pack('>I',0x12345678)  #四字節存入,按大端存儲
Character Byte order Size and alignment
@ native native 湊夠4個字節
= native standard 按原字節數
< little-endian 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('<I',0x12345678)  #四字節存入,按小端存儲
    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()

效果圖:
在這裏插入圖片描述

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()

效果圖:

在這裏插入圖片描述

學習:

  1. https://blog.csdn.net/and_then111/article/details/86744938
  2. python手冊
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章