python写二进制大文件_Python:切片非常大的二进制文件

Say I have a binary file of 12GB and I want to slice 8GB out of the middle of it. I know the position indices I want to cut between.

How do I do this? Obviously 12GB won't fit into memory, that's fine, but 8GB won't either... Which I thought was fine, but it appears binary doesn't seem to like it if you do it in chunks! I was appending 10MB at a time to a new binary file and there are discontinuities on the edges of each 10MB chunk in the new file.

Is there a Pythonic way of doing this easily?

解决方案

Here's a quick example. Adapt as needed:

def copypart(src,dest,start,length,bufsize=1024*1024):

with open(src,'rb') as f1:

f1.seek(start)

with open(dest,'wb') as f2:

while length:

chunk = min(bufsize,length)

data = f1.read(chunk)

f2.write(data)

length -= chunk

if __name__ == '__main__':

GIG = 2**30

copypart('test.bin','test2.bin',1*GIG,8*GIG)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值