我有一个大的二进制文件(60GB),我想将其分成几个较小的文件。我迭代了该文件,并使用 fileObject.tell()
方法找到了要分割文件的点,所以现在我有一个包含 1000 个分割点的数组,称为 file_pointers
>。我正在寻找一种从这些分割点创建文件的方法,因此该函数如下所示:
def split_file(file_object, file_pointers):
# Do something here
它会为每个 block 创建文件。我看到了这个question ,但我担心Python的循环可能太慢了,而且我也觉得必须有某种内置函数应该类似。
最佳答案
这比我想象的要简单得多,但我会在这里发布我的答案,以防有人想要快速解决方案。以下是从 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)
关于python - 如何在 Python 中复制二进制文件的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755002/