Use either the naive open
method or shutil.copyfileobj
. Both methods copy in chunks without pre-allocating the final file size, making sure that any interruption will reflect the data size copied at that point. Methods like shutil.copy
, shutil.copy2
, and shutil.copyfile
can, depending on the OS, pre-allocate the destination file’s final size before copying the content. This can lead to the file size being set to the full size even if the data copy is incomplete, especially in Python 3.9 and later where optimizations vary by OS.