如何在 Python 中仅列出 zip 存档中的文件夹?

发布于 2024-11-17 22:48:24 字数 190 浏览 0 评论 0原文

如何仅列出 zip 存档中的文件夹? 这将列出存档中的每个文件夹和文件:

import zipfile
file = zipfile.ZipFile("samples/sample.zip", "r")
for name in file.namelist():
    print name

谢谢。

How can i list only the folders from a zip archive?
This will list every folfder and file from the archive:

import zipfile
file = zipfile.ZipFile("samples/sample.zip", "r")
for name in file.namelist():
    print name

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

决绝 2024-11-24 22:48:24

我不认为以前的答案是跨平台兼容的,因为他们假设 pathsep 是 / 正如一些评论中所述。他们还忽略子目录(这对 Pythonpadavan 可能重要也可能不重要......问题并不完全清楚)。怎么样:

import os
import zipfile

z = zipfile.ZipFile('some.zip', 'r')
dirs = list(set([os.path.dirname(x) for x in z.namelist()]))

如果你真的只想要顶级目录,那么将其与 agroszer 的答案结合起来作为最后一步:

topdirs = [os.path.split(x)[0] for x in dirs]

(当然,最后两个步骤可以合并:)

I don't think the previous answers are cross-platform compatible since they're assuming the pathsep is / as noted in some of the comments. Also they ignore subdirectories (which may or may not matter to Pythonpadavan ... wasn't totally clear from question). What about:

import os
import zipfile

z = zipfile.ZipFile('some.zip', 'r')
dirs = list(set([os.path.dirname(x) for x in z.namelist()]))

If you really just want top-level directories, then combine this with agroszer's answer for a final step:

topdirs = [os.path.split(x)[0] for x in dirs]

(Of course, the last two steps could be combined :)

淑女气质 2024-11-24 22:48:24

一种方法可能是这样做:

>>> [x for x in file.namelist() if x.endswith('/')]
<<< ['folder/', 'folder2/']

One way might be to do:

>>> [x for x in file.namelist() if x.endswith('/')]
<<< ['folder/', 'folder2/']
一抹淡然 2024-11-24 22:48:24

在 python 3 中,这假设将绝对路径提供给 ZipFile:

from zipfile import ZipFile

zip_f = ZipFile("./Filename.zip")

# All directories:
for f in zip_f.namelist():
    zinfo = zip_f.getinfo(f)
    if(zinfo.is_dir()):
        print(f)

# Only root directories:
root_dirs = []
for f in zip_f.namelist():
    zinfo = zip_f.getinfo(f)
    if zinfo.is_dir():
        # This is will work in any OS because the zip format
        # specifies a forward slash.
        r_dir = f.split('/')
        r_dir = r_dir[0]
        if r_dir not in root_dirs:
            root_dirs.append(r_dir)
for d in root_dirs:
    print(d)

In python 3, this assumes absolute paths are fed to ZipFile:

from zipfile import ZipFile

zip_f = ZipFile("./Filename.zip")

# All directories:
for f in zip_f.namelist():
    zinfo = zip_f.getinfo(f)
    if(zinfo.is_dir()):
        print(f)

# Only root directories:
root_dirs = []
for f in zip_f.namelist():
    zinfo = zip_f.getinfo(f)
    if zinfo.is_dir():
        # This is will work in any OS because the zip format
        # specifies a forward slash.
        r_dir = f.split('/')
        r_dir = r_dir[0]
        if r_dir not in root_dirs:
            root_dirs.append(r_dir)
for d in root_dirs:
    print(d)
荒芜了季节 2024-11-24 22:48:24

更多的内容

set([os.path.split(x)[0] for x in zf.namelist() if '/' in x])

是因为 python 的 zipfile 不仅仅存储文件夹

more along the lines

set([os.path.split(x)[0] for x in zf.namelist() if '/' in x])

because python's zipfile does not store just the folders

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文