Python 和 zipfile 模块 [英] Python and the zipfile module

查看:42
本文介绍了Python 和 zipfile 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Python 文档:

According to Python documentation:

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

ZipFile.extract(member[, path[, pwd]]) Extract a member from the archive to the current working directory; member must be its full name or a ZipInfo object). Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or a ZipInfo object. pwd is the password used for encrypted files.

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

I have large number of zipped files that each contain 1000 archived files in them. Using the function above I can extract only the files that I need from each zipped archive:

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 中,而是将它们存储在 TestingFolder 内新创建的文件夹 0009001_0010000.zip 中>.

Ideally I want the extracted files to be stored into TestingFolder, but instead they are stored in a newly created folder 0009001_0010000.zip inside TestingFolder.

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

How can I direct the extracted files directly into TestingFolder but without creating a new folder 0009001_0010000.zip?

推荐答案

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

Rather than use extract(), you can use ZipFile.open() and copy the file to a filename of your own choosing; use shutil.copyfileobj() to efficiently copy the data across:

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)

这篇关于Python 和 zipfile 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆