Skip to content

Instantly share code, notes, and snippets.

@Juddling
Created December 19, 2018 11:49
Show Gist options
  • Select an option

  • Save Juddling/24f433d43ab3ae8f9c912dc496d860f1 to your computer and use it in GitHub Desktop.

Select an option

Save Juddling/24f433d43ab3ae8f9c912dc496d860f1 to your computer and use it in GitHub Desktop.
Create an archive for each directory in a given directory
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