Skip to content

Instantly share code, notes, and snippets.

@MMohan1
Last active June 1, 2023 19:45
Show Gist options
  • Save MMohan1/0c7b4c51319023dbd37d1c4f6bc5260d to your computer and use it in GitHub Desktop.
Save MMohan1/0c7b4c51319023dbd37d1c4f6bc5260d to your computer and use it in GitHub Desktop.
recursively Extract zip files
import os
import zipfile
import re
def extract_nested_zip(zippedFile, toFolder):
""" Extract a zip file including any nested zip files
Delete the zip file(s) after extraction
"""
if not os.path.exists(zippedFile):
return
with zipfile.ZipFile(zippedFile, 'r') as zfile:
zfile.extractall(path=toFolder)
os.remove(zippedFile)
for root, dirs, files in os.walk(toFolder):
for filename in files:
if re.search(r'\.zip$', filename):
fileSpec = os.path.join(root, filename)
extract_nested_zip(fileSpec, root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment