How can i list only the folders in zip archive in Python?

Pythonpadavan picture Pythonpadavan · Jun 28, 2011 · Viewed 9.9k times · Source

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.

Answer

zeekay picture zeekay · Jun 28, 2011

One way might be to do:

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