首先找到" inf" zip中的(固定扩展名)文件

时间:2015-01-22 12:00:39

标签: python

我在根文件中有N个文件的zip文件(没有子目录) 使用zipfile,如何获取zip中的第一个.inf文件的名称? (例如,它可能是“info.inf”。)

3 个答案:

答案 0 :(得分:2)

来自https://docs.python.org/2/library/zipfile.html的Python文档:

  

ZipFile.namelist()

     

按名称返回档案成员列表。

所以只需迭代该列表并检查条目.endswith(".inf")

请注意,您可能还需要关注区分大小写。

示例摘录:

>>> archive = zipfile.ZipFile("path/to/archive")
>>> for filename in archive.namelist():
...     if filename.endswith(".inf"):
...         print "Found .inf file: " + filename
...         break

答案 1 :(得分:0)

from zipfile import ZipFile

archive= ZipFile('path/to/zipfile.zip')
print [info.filename for info in archive.filelist if info.filename.endswith('.inf')][0]

答案 2 :(得分:0)

from zipfile import ZipFile

with ZipFile('path/to/archive.zip') as archive:
    print next((fn for fn in archive.namelist() if fn.endswith('.inf')), None)
如果未包含此类文件,则

打印None