Created
December 19, 2018 11:49
-
-
Save Juddling/24f433d43ab3ae8f9c912dc496d860f1 to your computer and use it in GitHub Desktop.
Create an archive for each directory in a given directory
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
| import os.path | |
| import shutil | |
| directory = '.' | |
| backup_directory = '_archives' | |
| to_skip = [] | |
| def should_be_archived(file): | |
| if not os.path.isdir(file): | |
| return False | |
| if file == backup_directory: | |
| return False | |
| if file in to_skip: | |
| return False | |
| return True | |
| for file in os.listdir(directory): | |
| if should_be_archived(file): | |
| output_file = os.path.join(backup_directory, file) | |
| try: | |
| shutil.make_archive(output_file, 'zip', file) | |
| except ValueError: | |
| print("error creating archive for {}".format(output_file)) | |
| continue | |
| print("created archive {}".format(output_file)) | |
| print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment