Python and the zipfile module

烂漫一生 提交于 2019-12-02 19:31:17

问题


According to Python documentation:

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.

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")

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.

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


回答1:


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)


来源:https://stackoverflow.com/questions/28224916/python-and-the-zipfile-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!