Question

How can I create a .tar.gz file with compression in Python?

Was it helpful?

Solution

To build a .tar.gz (aka .tgz) for an entire directory tree:

import tarfile

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

OTHER TIPS

import tarfile
tar = tarfile.open("sample.tar.gz", "w:gz")
for name in ["file1", "file2", "file3"]:
    tar.add(name)
tar.close()

If you want to create a tar.bz2 compressed file, just replace file extension name with ".tar.bz2" and "w:gz" with "w:bz2".

You call tarfile.open with mode='w:gz', meaning "Open for gzip compressed writing."

You'll probably want to end the filename (the name argument to open) with .tar.gz, but that doesn't affect compression abilities.

BTW, you usually get better compression with a mode of 'w:bz2', just like tar can usually compress even better with bzip2 than it can compress with gzip.

Previous answers advise using tarfile python module for creating a .tar.gz file in python. That's obviously a good and python-style solution, though it has serious drawback in speed of the archiving. This question mentions, that tarfile is approximately twice slower, than calling direct command in Linux. According to my experience this estimation is pretty correct.

So for faster archiving you can use direct Linux command using subprocess module:

subprocess.call(['tar', '-czf', output_filename, file_to_archive])

In this tar.gz file compress in open view directory In solve use os.path.basename(file_directory)

with tarfile.open("save.tar.gz","w:gz"):
      for file in ["a.txt","b.log","c.png"]:
           tar.add(os.path.basename(file))

its use in tar.gz file compress in directory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top