How can I create a full compressed .tar.gz
file using Python tar?
Using the tarfile
module (recommended): With over a decade of Python experience, I can confidently say the built-in tarfile
module is your go-to for creating .tar.gz
files. It’s straightforward and reliable. Here’s a simple example:
import tarfile
def create_tar_gz(file_name, source_dir):
with tarfile.open(file_name, "w:gz") as tar:
tar.add(source_dir, arcname=".")
create_tar_gz("compressed_file.tar.gz", "path/to/directory")
In this solution, tarfile.open
opens the .tar.gz
file in write mode with gzip compression ("w:gz"
). Then, the add()
method includes the contents of the directory in the archive. This exact match keyword solution is great if you want full control over the compression process.
Adding to Vindhya’s excellent approach, if you’re looking for something even simpler—especially for directories—the shutil
module is worth a look. As someone who’s leaned on Python for automation over the years, I find this method handy for quick tasks:
import shutil
def create_tar_gz(file_name, source_dir):
shutil.make_archive(file_name, "gztar", source_dir)
create_tar_gz("compressed_file", "path/to/directory")
This exact match keyword solution uses the make_archive()
function from shutil
, which automatically handles creating and compressing the .tar.gz
file. It’s less granular than tarfile
, but perfect for everyday use when you want a no-fuss solution.
Building on the earlier approaches, there’s another powerful way if you’re comfortable with external tools. I’ve often turned to the subprocess
module to call system commands directly when I needed speed or specific system-level optimizations. Here’s how you can use it:
import subprocess
def create_tar_gz(file_name, source_dir):
subprocess.run(["tar", "-czf", file_name, "-C", source_dir, "."])
create_tar_gz("compressed_file.tar.gz", "path/to/directory")
This exact match keyword method invokes the system’s tar
command, where -czf
specifies gzip compression and archive creation. It’s particularly useful when you want to integrate directly with Unix-like systems or need features beyond what Python libraries offer. Just ensure the tar
utility is available on your system.