【问题标题】:How do I copy a chunk of a binary file in Python?如何在 Python 中复制一大块二进制文件?
【发布时间】:2017-10-15 12:23:49
【问题描述】:

我有一个大的二进制文件 (60GB),我想将它分成几个较小的文件。我遍历了文件并找到了我想要使用fileObject.tell() 方法分割文件的点,所以现在我有一个名为file_pointers 的包含1000 个分割点的数组。我正在寻找一种从这些分割点创建文件的方法,所以函数看起来像:

def split_file(file_object, file_pointers):
     # Do something here

它会为每个块创建文件。我看到了这个question,但我担心Python的循环可能太慢了,我也觉得必须有某种内置函数应该类似。

【问题讨论】:

    标签: python file buffer binaryfiles


    【解决方案1】:

    这比我想象的要简单得多,但我会在这里发布我的答案,以防万一有人想要快速解决方案。下面是一个从file_pointer[1]复制到file_pointer[2]的例子

    with open('train_example.bson', 'rb') as fbson:
        fbson.seek(file_pointers[1])
        bytes_chunk = fbson.read(file_pointers[2] - file_pointers[1])
        with open('tmp.bson', 'wb') as output_file:
            output_file.write(bytes_chunk)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2019-06-18
      • 2022-10-25
      相关资源
      最近更新 更多