Created
October 15, 2017 17:51
-
-
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
This file contains hidden or 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
''' | |
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...