Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all files inside a folder in a zip file in python

Tags:

python

zip

I have a zip file structure like - B.zip/org/note.txt I want to directly list the files inside org folder without going to other folders in B.zip

I have written the following code but it is listing all the files and directories available inside the B.zip file

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

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

1 Answers

You can filter the yields by startwith function.(Using 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)
like image 65
Rao Sahab Avatar answered Feb 23 '26 17:02

Rao Sahab