Skip to content

Instantly share code, notes, and snippets.

@slavama
Created September 12, 2018 08:20
Show Gist options
  • Save slavama/e4fe8797d38dfd294370d945aee5786b to your computer and use it in GitHub Desktop.
Save slavama/e4fe8797d38dfd294370d945aee5786b to your computer and use it in GitHub Desktop.
tar.gz archiving in python
import os
import tarfile
from contextlib import closing
dirs = ['/var/www/clients/site1', '/var/www/clients/site2']
for d in dirs:
contents = os.listdir(d)
name = d.split('/')[4]
print (name)
with closing(tarfile.open('%s.tar.gz' % name, 'w:gz')) as tar:
for item in contents:
tar.add(os.path.join(d, item))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment