Skip to main content
 首页 » 编程设计

python之使用python解压大文件

2025年05月04日7itcoder

我正在尝试使用 python 解压缩各种大小的文件(有些大小为 4GB 或更大),但是我注意到在某些情况下,尤其是当文件非常大时,文件无法解压缩。当我打开新的结果文件时,它是空的。以下是我正在使用的代码 - 我的方法有什么问题吗?

        inF = gzip.open(localFile, 'rb') 
        localFile = localFile[:-3] 
        outF = open(localFile, 'wb') 
        outF.write( inF.read() ) 
        inF.close() 
        outF.close() 

请您参考如下方法:

在这种情况下,看起来您不需要 python 对您读入的文件进行任何处理,因此您最好只使用 subprocess.Popen:

from subprocess import Popen 
Popen('gunzip %s %s' % (infilename, outfilename)).wait() 

您可能需要传递 shell=True,但除此之外应该不错