我在一个zip文件中列出了300万个html文件。我想从整个文件列表中提取〜4000个html文件。有没有一种方法可以提取特定文件而无需使用Python解压缩整个zipfile?
任何线索都将不胜感激!预先感谢。
编辑:不好,我应该详细说明这个问题。我列出了所有需要提取的html文件名,但它们分布在12个zip文件中。如何遍历每个zip文件,提取匹配的html文件并获取提取的html文件的最终列表?
假设您希望提取所有html
文件,然后就可以提取出来。如果您有要提取的所有文件名的列表,则将需要稍作修改。
listOfZipFiles = ['sample1.zip', 'sample2.zip', 'sample1.zip',... , 'sample12.zip' ]
fileNamesToBeExtracted = ['file1.html', 'file2.html', ... 'filen.html']
# Create a ZipFile Object and load sample.zip in it
for zipFileName in listOfZipFiles:
with ZipFile(zipFileName, 'r') as zipObj:
# Get a list of all archived file names from the zip
listOfFileNames = zipObj.namelist()
# Iterate over the file names
for fileName in listOfFileNames:
# Check if file to be extracted is present in file names to be extracted
if fileName in fileNamesToBeExtracted:
# Extract a single file from zip
zipObj.extract(fileName)