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

Question:

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:pythonB.jar')

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

||

Answers:

You can filter the yields by startwith function.(Using Python 3)

import os
import zipfile

with zipfile.ZipFile('D:pythonB.jar') as z:
    for filename in z.namelist():
        if filename.startswith("org"):
            print(filename)
Answered By: Rao Sahab

How to list all files that are inside ZIP files of a certain folder

Everytime I came into this post making a similar question… But different at the same time. Cause of this, I think other users can have the same doubt. If you got to this post trying this….

import os
import zipfile

# Use your folder path 
path = r'set_yoor_path'

for file in os.listdir(os.chdir(path)):
    if file[-3:].upper() == 'ZIP':
        for item in zipfile.ZipFile(file).namelist():
            print(item)

If someone feels that this post has to be deleted, please let m know. Tks

Answered By: Andre Nevares
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.