fileheaders = b'\x88\x88\x88\x88\x88'
filetails = b'\x99\x99\x99\x99\x99\x99\x99\x99\x99'
resultfile = r'.\data\result'
def read_in_block(file_path, resultfile):
BLOCK_SIZE = 16#设置每次读取 16个字节
ended = -1
started = -1
with open(file_path, "rb") as r:
with open(resultfile, "wb+") as w:
block = r.read(BLOCK_SIZE)
if not block: return
while True:
blockNext = r.read(BLOCK_SIZE)#每次读取固定长度到内存缓冲区
if blockNext:
buff = block + blockNext
if started <= -1:
# 没有开始,继续查找开始标志
started = buff.find(fileheaders)
#处理逻辑
if started > -1:
# 已经开始,继续查找结束标志
ended = buff.find(filetails)
# 如果结束标志找到,修正结束位置
if ended > -1: ended += len(filetails)
# 写入文件,找到结束位置,写buff,只找到开始位置,写block
a = buff if ended > -1 else block
w.write(a)
# 修正开始位置为下一block的开始位置
started = 0 if started < BLOCK_SIZE else started - BLOCK_SIZE
if ended > -1:
# 已经结束
break
# 缓存数据
block = blockNext
else:
return#如果读取到文件末尾,则退出
read_in_block(filePath1, resultfile)
print('运行结束') open的时候,可以不指定buffer,不会内存异常,因为open动作没有真正的读取文件内容,只有在read操作时才需要指定buffer,不然会默认读取所有内容报内存异常。
页:
[1]