Last active
October 24, 2021 01:36
-
-
Save tkrsh/307ddf82b7a0488e06b1ffd0bac15c84 to your computer and use it in GitHub Desktop.
Extract multiple zip files at once (python3 extractor.py /path/to/directory_with_zips)
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
""" | |
system imports | |
""" | |
import os | |
import sys | |
import logging | |
def get_zip_file_names(path): | |
""" | |
takes path of directory and returns a list of zip files in the directory | |
""" | |
zip_files = [] | |
for file in os.listdir(path): | |
if file.endswith(".zip"): | |
zip_files.append(file) | |
return zip_files | |
def extract_zip_file(zip_file_name): | |
""" | |
takes zip file name and path and extracts the zip file to the path | |
""" | |
try: | |
os.system("unzip " + zip_file_name) | |
except: | |
logging.error("Error extracting zip file") | |
sys.exit(1) | |
def delete_zip_file(zip_file_name): | |
""" | |
takes zip file name and deletes the zip file | |
""" | |
try: | |
os.remove(zip_file_name) | |
except: | |
logging.error("Error deleting zip file") | |
sys.exit(1) | |
def process(): | |
""" | |
main function that processes the zip files | |
""" | |
zip_files = get_zip_file_names(sys.argv[1]) | |
for file in zip_files: | |
extract_zip_file(file) | |
delete_zip_file(file) | |
if __name__ =='__main__': | |
os.chdir(sys.argv[1]) | |
process() | |
logging.info("Extraction Completed") | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment