'zipfile list of content from multiple paths
I'm trying to get the list of all files in .zip that are in many different folders
To identify the paths of all .zip files I used this code:
import os, zipfile
path1 = 'C:\\desktop\\somefolderpath'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path1):
for file in f:
if '.zip' in file:
files.append(os.path.join(r, file))
for f in files:
print(f)
with that I get the whole list:
C:\desktop\somefolderpath\1\folder1\zipfile1.zip
C:\desktop\somefolderpath\2\folder3\zipfile5.zip
...
And then I used .namelist to get the list of contents in certain .zip
zipPath = 'C:\\desktop\\somefolderpath\\1\\folder1\\zipfile1.zip'
zip = zipfile.ZipFile(zipPath)
print(zip.namelist())
How can I now use paths from "f" instead of writing paths everytime?
Solution 1:[1]
You can just iterate through the list as you have already done for printing out the filepaths-
import os, zipfile
path1 = 'C:\\desktop\\somefolderpath'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path1):
for file in f:
if '.zip' in file:
files.append(os.path.join(r, file))
for zipPath in files:
zip = zipfile.ZipFile(zipPath)
print(zip.namelist())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cool Developer |