I am trying to write a script which can unzip something like this:
- Great grandfather.zip
- Grandfather.zip
- Father.zip
- Child.txt
- Father.zip
- Grandfather.zip
What I have so far:
from os import listdir
import os
from zipfile import ZipFile, is_zipfile
#Current Directory
mypath = '.'
def extractor(path):
for file in listdir(path):
if(is_zipfile(file)):
print(file)
with ZipFile(file,'r') as zipObj:
path = os.path.splitext(file)[0]
zipObj.extractall(path)
extractor(path)
extractor(mypath)
I can unzip great grandfather and when I call the extractor again with grandfather as path. It doesn’t go inside the if statement. Even though, I can list the contents of grandfather.
>Solution :
Replace extractor(path)
by these two lines:
os.chdir(path)
extractor('.')
So your code becomes:
from os import listdir
import os
from zipfile import ZipFile, is_zipfile
#Current Directory
mypath = '.'
def extractor(path):
for file in listdir(path):
if(is_zipfile(file)):
print(file)
with ZipFile(file,'r') as zipObj:
path = os.path.splitext(file)[0]
zipObj.extractall(path)
os.chdir(path)
extractor('.')
extractor(mypath)