代码
import struct
import os
import sys
if __name__ == '__main__':
# wav文件
filepath='welcome_01.wav'
# 打开二进制文件
bin_file = open(filepath, 'rb')
# 跳到data
bin_file.seek(42,0)
# 读取data大小
data=bin_file.read(4)
# 转10进制,小端对齐
size = int().from_bytes(data, byteorder="little", signed=True)
# 文件个数
num=0
# 每次写入个数
each_size=10240
print(size)
# 循环写入 each_size 个字节到 output.txt
while size>=each_size:
# 重定向输出到指定文件
file = open('output{:d}.txt'.format(num), 'w')
sys.stdout = file
# 读取指定个数字节
data = bin_file.read(each_size)
print("size ",10240)
for i, index in zip(data, range(1, each_size + 1)):
# 每 16 个 字节换行一次
if index % 16 == 0:
print("0x%02x ," % i)
else:
print("0x%02x ," % i, end='')
file.close()
size-=each_size
num+=1
# 写入剩余字节
file = open('output{:d}.txt'.format(num), 'w')
sys.stdout = file
data = bin_file.read(size)
print("size ", size)
for i, index in zip(data, range(1, size + 1)):
if index % 16 == 0:
print("0x%02x ," % i)
else:
print("0x%02x ," % i, end='')
file.close()
bin_file.close()