Created
September 15, 2014 14:23
-
-
Save JamesPHoughton/0f4f269e93a2b85958d8 to your computer and use it in GitHub Desktop.
Recursively unpack zip files in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from zipfile import ZipFile | |
def unpack_zip(zipfile='', path_from_local=''): | |
filepath = path_from_local+zipfile | |
extract_path = filepath.strip('.zip')+'/' | |
parent_archive = ZipFile(filepath) | |
parent_archive.extractall(extract_path) | |
namelist = parent_archive.namelist() | |
parent_archive.close() | |
for name in namelist: | |
try: | |
if name[-4:] == '.zip': | |
unpack_zip(zipfile=name, path_from_local=extract_path) | |
except: | |
print 'failed on', name | |
pass | |
return extract_path | |
# you can just call this with filename set to the relative path and file. | |
path = unpack_zip(filename) |
i recommend you to not try this on 42.zip(your computer probably can't handle a few petabytes)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had a zip file full of zip files, each of which might contain zip files, and I wanted a matching folder structure.
Wrote this for a mac.