【问题标题】:List all files inside a folder in a zip file in python列出python中zip文件中文件夹内的所有文件
【发布时间】:2018-04-03 08:18:56
【问题描述】:

我有一个 zip 文件结构,例如 - B.zip/org/note.txt 我想直接列出 org 文件夹中的文件,而不去 B.zip 中的其他文件夹

我编写了以下代码,但它列出了 B.zip 文件中可用的所有文件和目录

f = zipfile.ZipFile('D:\python\B.jar')

for name in f.namelist():
    print '%s: %r' % (name, f.read(name))

【问题讨论】:

    标签: python zip


    【解决方案1】:

    您可以通过 startwith 函数过滤收益。(使用 Python 3)

    import os
    import zipfile
    
    with zipfile.ZipFile('D:\python\B.jar') as z:
        for filename in z.namelist():
            if filename.startswith("org"):
                print(filename)
    

    【讨论】:

    • 如果我在 org 内部还有一个目录,比如 B.zip/org/new/note.txt
    • 代替 if filename.startswith("org"): 使用 if filename.startswith("org/new"):
    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多