使用 python 限制 bz2 文件解压缩?

Limit on bz2 file decompression using python?

我有许多以 bz2 格式压缩的文件,我正尝试使用 python 将它们解压缩到一个临时目录中,然后进行分析。有几十万个文件,手动解压文件不可行,所以我写了下面的脚本。

我的问题是,每当我尝试执行此操作时,最大文件大小为 900 kb,即使手动解压缩每个文件大约 6 MB。我不确定这是否是我的代码中的缺陷以及我如何将数据保存为字符串然后复制到文件或其他问题。我用不同的文件试过这个,我知道它适用于小于 900 kb 的文件。有没有其他人遇到过类似的问题并且知道解决方案?

我的代码如下:

import numpy as np
import bz2
import os
import glob

def unzip_f(filepath):
    '''
    Input a filepath specifying a group of Himiwari .bz2 files with common names
    Outputs the path of all the temporary files that have been uncompressed

    '''


    cpath = os.getcwd() #get current path
    filenames_ = []  #list to add filenames to for future use

    for zipped_file in glob.glob(filepath):  #loop over the files that meet the name criterea
        with bz2.BZ2File(zipped_file,'rb') as zipfile:   #Read in the bz2 files
            newfilepath = cpath +'/temp/'+zipped_file[-47:-4]     #create a temporary file
            with open(newfilepath, "wb") as tmpfile: #open the temporary file
                for i,line in enumerate(zipfile.readlines()):
                    tmpfile.write(line) #write the data from the compressed file to the temporary file



            filenames_.append(newfilepath)
    return filenames_


path_='test/HS_H08_20180930_0710_B13_FLDK_R20_S*bz2'
unzip_f(path_)   

它 returns 正确的文件路径,但大小上限为 900 kb。

原来这个问题是由于文件是多流的,这在 python 2.7 中不起作用。还有更多信息 here as mentioned by jasonharper and here。下面是一个解决方案,只使用 Unix 命令解压 bz2 文件,然后将它们移动到我想要的临时目录。它不是那么漂亮,但它有效。

import numpy as np
import os
import glob
import shutil

def unzip_f(filepath):
    '''
    Input a filepath specifying a group of Himiwari .bz2 files with common names
    Outputs the path of all the temporary files that have been uncompressed

    '''


    cpath = os.getcwd() #get current path
    filenames_ = []  #list to add filenames to for future use

    for zipped_file in glob.glob(filepath):  #loop over the files that meet the name criterea
        newfilepath = cpath +'/temp/'   #create a temporary file
        newfilename = newfilepath + zipped_file[-47:-4]

        os.popen('bzip2 -kd ' + zipped_file)
        shutil.move(zipped_file[-47:-4],newfilepath)

        filenames_.append(newfilename)
    return filenames_



path_='test/HS_H08_20180930_0710_B13_FLDK_R20_S0*bz2'

unzip_f(path_)   

这是 Python2 中的已知限制,其中 BZ2File class 不支持多个流。 这可以通过使用 bz2filehttps://pypi.org/project/bz2file/ 轻松解决,它是 Python3 实现的后向移植,可以用作直接替换。

在 运行 pip install bz2file 之后你可以用它替换 bz2import bz2file as bz2 一切正常:)

原始 Python 错误报告:https://bugs.python.org/issue1625