如何使用python压缩300GB文件

2024-10-01 02:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试压缩大小为300GB的虚拟机文件。在

每次python脚本被终止时,因为 gzip模块超过30GB(虚拟内存)。在

有没有办法用python实现大文件(300GB到64TB)压缩?在

def gzipFile(fileName):
  startTime = time.time()
  with  open(fileName,'rb') as fileHandle:
     compressedFileName = "%s-1.gz" % fileName
     with gzip.open(compressedFileName, 'wb') as compressedFH:
        compressedFH.writelines(fileHandle)

  finalTime = time.time() - startTime
  print("gzipFile=%s fileName=%s" % (finalTime,compressFileName))

Tags: 模块文件脚本timeaswithopenfilename
2条回答
from subprocess import call
call(["tar", "-pczf name_of_your_archive.tar.gz /path/to/directory"])

从外部运行,最简单的方式,可能最快。在

with gzip.open(compressedFileName, 'wb') as compressedFH:
    compressedFH.writelines(fileHandle)

逐行写入文件fileHandle,即将其拆分为由\n字符分隔的块。在

虽然这个字符很有可能在二进制文件中不时出现,但这并不能保证。在

这样做可能更好

^{pr2}$

或者,正如tqzf在评论中所写

with gzip.open(compressedFileName, 'wb') as compressedFH:
    shutil.copyfileobj(fileHandle, compressedFileName)

相关问题 更多 >