如何在 Python 中仅列出 zip 存档中的文件夹?
如何仅列出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为以前的答案是跨平台兼容的,因为他们假设 pathsep 是
/
正如一些评论中所述。他们还忽略子目录(这对 Pythonpadavan 可能重要也可能不重要......问题并不完全清楚)。怎么样:如果你真的只想要顶级目录,那么将其与 agroszer 的答案结合起来作为最后一步:
(当然,最后两个步骤可以合并:)
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:If you really just want top-level directories, then combine this with agroszer's answer for a final step:
(Of course, the last two steps could be combined :)
一种方法可能是这样做:
One way might be to do:
在 python 3 中,这假设将绝对路径提供给 ZipFile:
In python 3, this assumes absolute paths are fed to
ZipFile
:更多的内容
是因为 python 的 zipfile 不仅仅存储文件夹
more along the lines
because python's zipfile does not store just the folders