my scipt is working as in searching for zip files, unzip and do the things i want. But problem arise when i have nested zip files inside the zip file, so i thought maybe i copy the working if statement, make a few adjustments but i still cant get it to work.
JavaScript
x
25
25
1
print('Searching for ZipFiles')
2
for file in os.listdir(working_directory):
3
zfile = file
4
if zfile.endswith('.zip'):
5
# Create a ZipFile Object and load sample.zip in it
6
with ZipFile(zfile, 'r') as zipObj:
7
# Get a list of all archived file names from the zip
8
listOfFileNames = zipObj.namelist()
9
# Iterate over the file names
10
for fileName in listOfFileNames:
11
zipObj.extract(fileName, './temp')
12
maincom() #until here, my script is working, below is the new IF statement
13
if fileName.endswith('.zip'):
14
for file in os.listdir('.'):
15
zfile = file
16
if zfile.endswith('.zip'):
17
# Create a ZipFile Object and load sample.zip in it
18
with ZipFile(zfile, 'r') as zipObj:
19
# Get a list of all archived file names from the zip
20
listOfFileNames = zipObj.namelist()
21
# Iterate over the file names
22
for fileName in listOfFileNames:
23
zipObj.extract(fileName, '')
24
maincom()
25
what i want to achieve is to simply unzip the nested zip files in the current directory they are found, run maincom(), if possible, maybe delete the nested zip file after unzip completion
Thanks guys
Advertisement
Answer
Try this:
JavaScript
1
10
10
1
import glob
2
import zipfile
3
4
while len(glob.glob(working_directory+"*zip")) != 0:
5
for filename in glob.glob(working_directory+"*zip"):
6
with ZipFile(filename, 'r') as zipObj:
7
for fileName in zipObj.namelist():
8
zipObj.extract(fileName, './temp')
9
maincom()
10
In principle, this will loop until there is no more zipfile in your working directory.
Which means: if zipfiles are present at iteration 1 and contain other zipfiles, those will be extracted. Which means: zipfiles will be present at iteration 2. And will be extracted. Etc Etc…