简体   繁体   English

如何使用python将大文件从源文件分块复制到目标文件?

[英]How do I copy a large file in chunks from a source file to a destination file using python?

The file can be say 20GB.该文件可以说是 20GB。 The traditional approach of importing,reading a file and then writing to another file is not feasible.传统的导入、读取文件然后写入另一个文件的方法是不可行的。

The shutil module has a function designed for this purpose: shutil模块具有为此目的设计的功能:

import shutil
shutil.copy(src, dst)

For file-like objects (buffers, request bodies, etc.) you can use shutil.copyfileobj对于类文件对象(缓冲区、请求主体等),您可以使用shutil.copyfileobj

with open('path/to/dest.file', 'wb') as dst:
    shutil.copyfileobj(source_file_like_object, dst)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM