Zip包含一个档案文件夹,如何使用Python获取档案中每个zip的注释?

时间:2018-08-11 15:22:50

标签: python zip

我有一个ZIP文件,其中包含一个包含很多zip文件的文件夹(我不确定这些文件是否为zip文件,因为它们以。(Number)结尾,例如.100,.812等。) 包含注释,如何使用python获取每个注释?

我尝试使用此代码:

import zipfile
archive = zipfile.ZipFile(r'D:\XXX\Desktop\MyZip\archives', 'r') 
for i in range (20):
   print(archive.getinfo("unzipme."+str(i)).comment)

如您所见,“大” zip文件是MyZip,其中的文件夹是“档案”

错误:

Traceback (most recent call last):
  File "D:/XXX/PycharmProjects/helper/help1.py", line 2, in <module>
    archive = zipfile.ZipFile(r'D:\XXX\Desktop\MyZip\archives', 'r')
  File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\zipfile.py", line 752, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'D:\\XXX\\Desktop\\MyZip\\archives'

但是它不起作用...任何帮助将不胜感激!谢谢:)

1 个答案:

答案 0 :(得分:0)

您必须遍历目录并分别检查所有zip文件,以获得每个存档的注释:

import zipfile, os
for file in os.listdir("/home/user/archives"):
    if file.endswith(".zip"):
        archive = zipfile.ZipFile(file, 'r') 
        print "%s: %s" % (file,archive.comment,)

将输出

SevSeg-3.3.0.zip: 4d6b8654353bfeab69efaf90e8f14c8d37b98101
adafruit-Adafruit-Motor-Shield-library-1.0.0-3-g99381df.zip: 99381df8ba1dd440c85bacaadb13d016158e7bf8
LiquidCrystal_PCF8574-master.zip: 9236b861ae7b68062f71741c91bca5936689c30e