Skip to content

Instantly share code, notes, and snippets.

@MichaelSEA
Created October 15, 2017 17:51
Show Gist options
  • Save MichaelSEA/a73ad1a84c05471a2f9bae7bd37094e2 to your computer and use it in GitHub Desktop.
Save MichaelSEA/a73ad1a84c05471a2f9bae7bd37094e2 to your computer and use it in GitHub Desktop.
python script to crawl directories, unzip files where found and then delete them
'''
walk a file tree from specified starting point, unzip any zip files encountered in the
directory where zip file was found, and then delete the zip file after succesfully
unzipping
'''
import os
import zipfile
folder_root = '/Users/me/Pictures'
suffix = ".zip"
os.chdir(folder_root)
for folder, dirs, files in os.walk(folder_root, topdown=False):
for name in files:
if name.endswith(suffix):
# print(os.path.join(folder, name))
# unzip to folder and delete the zip file
os.chdir(folder)
zip_file = zipfile.ZipFile(os.path.join(folder, name))
zip_file.extractall()
zip_file.close()
os.remove(os.path.join(folder, name))
@askfriends
Copy link

can you make a python script to create .zip of al su-folders in a directory as individual folders.zip
Like Folder1.zip, Folder2.zip. Folder3.zip and so on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment