Python和zipfile模块

时间:2015-01-29 21:26:46

标签: python python-2.7 zipfile

根据Python文档:

  

ZipFile.extract(member [,path [,pwd]])   将成员从存档中提取到当前工作目录;成员必须是其全名或ZipInfo对象)。它的   尽可能准确地提取文件信息。的路径   指定要提取到的其他目录。会员可以是   filename或ZipInfo对象。 pwd是用于加密的密码   文件。

我有大量的压缩文件,每个文件包含1000个存档文件。使用上面的函数,我只能从每个压缩存档中提取我需要的文件:

def getAIDlist(aidlist_to_keep,ifile,folderName):

    archive = zipfile.ZipFile(ifile) #
    aidlist=archive.namelist() # gets the names of all files in the zipped archive

    print "AIDs to keep",aidlist_to_keep

    print  "Number of AIDs in the zipped archive ",len(aidlist)

    path='/2015/MyCODE/'+folderName

    for j in aidlist_to_keep:
        for k in aidlist:
            if j in k:
                try:
                    archive.extract(k,path)
                except:
                    print "Could Not Extract file ",(j)
                    pass

    return
if __name__ == '__main__':
    getAIDlist(['9593','9458','9389'],"0009001_0010000.zip","TestingFolder")

理想情况下,我希望将提取的文件存储到TestingFolder中,而是将它们存储在0009001_0010000.zip内新创建的文件夹TestingFolder中。

如何将提取的文件直接导入TestingFolder但不创建新文件夹0009001_0010000.zip

1 个答案:

答案 0 :(得分:3)

您可以使用ZipFile.open()而不是使用extract(),并将文件复制到您自己选择的文件名中;使用shutil.copyfileobj()有效地复制数据:

import shutil

archive = zipfile.ZipFile(ifile)
path = os.path.join('/2015/MyCODE', folderName)

for name in aidlist_to_keep:
    try:
        archivefile = archive.open(name)
    except KeyError:
        # no such file in the archive
        continue
    with open(os.path.join(path, name), 'wb') as targetfile:
        shutil.copyfileobj(archivefile, targetfile)