Skip to content

Instantly share code, notes, and snippets.

@pavgup
Last active January 30, 2020 15:33
Show Gist options
  • Save pavgup/54211a6104fcf5fe63bf05d72dc1c95d to your computer and use it in GitHub Desktop.
Save pavgup/54211a6104fcf5fe63bf05d72dc1c95d to your computer and use it in GitHub Desktop.
recursive zip file extraction with dcm dropout? woo!
import zipfile
from pathlib import Path
# Pathlib Friendly (maybe required)
def extract(filename):
z = zipfile.ZipFile(filename)
for file in z.namelist():
# Ignore anything that's not a zip... and lets recurse
if ".zip" in file.lower():
# Create a subdirectory to hold our zip:
newDirectory = filename.parents[0]/file.split('.')[0]
newDirectory.mkdir(parents=True, exist_ok=True)
#If you need debug statements to spam you, uncomment this
#print("Making the directory " + (filename.parents[0]/file.split('.')[0]).as_posix() + "and unzipping into it...")
# Lets place this zip into the new directory:
z.extract(file,newDirectory.as_posix())
# Send this new zip back through this function, lets get recursive
extract(newDirectory/file)
# If it's not a zip we're looking at, lets extract DCMs to our working directory
elif ".dcm" in file.lower():
z.extract(file,filename.parents[0].as_posix())
filename = Path("./data/aFolderThatContainsZips")
extract(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment