问题
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