How to Use the Python tarfile Module

Python's built-in tarfile module provides a comprehensive suite of tools for reading, writing, and manipulating TAR archives, including those compressed with gzip, bz2, and lzma/xz formats. This article covers what the tarfile module accomplishes, its primary capabilities—such as inspecting archive contents, extracting files safely, and creating new archives—and practical examples of how to implement these features in your Python workflows without relying on external system utilities.

Core Capabilities of the tarfile Module

The tarfile module eliminates the need to shell out to system-level tar commands. It interacts directly with standard POSIX tar archives, handling low-level byte manipulation, header parsing, and directory structures natively.

1. Reading and Inspecting Archives

The module allows you to inspect the contents of an archive without extracting it to disk first. Using tarfile.open(), you can open an archive and inspect metadata such as file names, permissions, modification times, and file sizes.

import tarfile

with tarfile.open("backup.tar.gz", "r:gz") as tar:
    # Print all file names in the archive
    for name in tar.getnames():
        print(name)

2. Creating New Archives

The tarfile module allows you to create uncompressed or compressed archives and add files or entire directories into them using the add() method.

import tarfile

# Create a gzip-compressed archive
with tarfile.open("archive.tar.gz", "w:gz") as tar:
    # Add a single file
    tar.add("document.pdf")
    # Add an entire directory recursively
    tar.add("my_folder", arcname="stored_folder")

The arcname parameter allows you to rename the file or directory inside the archive so that local system paths are not exposed.

3. Extracting Archive Contents

Files can be extracted either individually or all at once. The module handles directory creation and file permissions automatically during extraction.

import tarfile

with tarfile.open("archive.tar.gz", "r:gz") as tar:
    # Extract everything to a specific folder
    tar.extractall(path="./extracted_files")

Note: In modern Python versions (3.12+), you can use the filter parameter (such as filter='data') with extractall() to prevent directory traversal vulnerabilities like "Zip Slip."

4. Built-in Compression Support

The module handles compression transparently by changing the open mode flag passed to tarfile.open():

Summary

The tarfile module serves as a complete, self-contained solution for archive management in Python. It enables scripts to package datasets, read compressed logs in-memory, generate downloadable backups, and unpack incoming files across Windows, macOS, and Linux without external dependencies.