Last Updated: February 25, 2016
·
517
· darthlukan

Create gzipped tarballs with Python

Here's a little something that I've been using a lot lately on our servers. I use it mostly for compressing database backups prior to sending them off to an S3 bucket.

# We use this later for looping over and sending archived names.
archives = []

def compress(source):
    target = "%s.%s" % (source, 'tgz')
    with tarfile.open(target, "w:gz") as tgz:
        tgz.add(source, arcname=target)
        archives.appent(target)
    tgz.close()

Simple and to the point, just as code should be.