How to create a tar file using Python?

TAR stands for Tape Archive Files. Tar files are archive files that allow us to store multiple files and directories in a single file. Open-source software is commonly distributed using tar files.

Tar files typically end in .tar and can be compressed using tools such as gzip, resulting in files ending with .tar.gz.

File Modes for Creating Tar Files

Here are the available file modes to create tar files using Python's tarfile module ?

  • "w": Write a tar archive without compression.
  • "w:gz": Write a gzip-compressed archive.
  • "w:bz2": Write a bzip2-compressed archive.
  • "w:xz": Write an xz-compressed archive (Python 3.3+).

Creating a Basic .tar File

The tarfile module provides a simple interface for creating tar archives. To create a .tar file, open the archive in write mode and add files using the add() method.

Example

The following example creates a tar file and adds files to it ?

import tarfile
import os

# Create sample files for demonstration
with open("notes.txt", "w") as f:
    f.write("Sample notes content")

with open("report.txt", "w") as f:
    f.write("Sample report content")

# Create a .tar archive file
with tarfile.open("TutorialsPoint.tar", "w") as tar:
    tar.add("notes.txt")
    tar.add("report.txt")

print("Created TutorialsPoint.tar successfully")

# Verify the contents
with tarfile.open("TutorialsPoint.tar", "r") as tar:
    print("Archive contents:", tar.getnames())
Created TutorialsPoint.tar successfully
Archive contents: ['notes.txt', 'report.txt']

Creating a Compressed .tar.gz File

To create a compressed .tar.gz file, use the "w:gz" mode. This applies gzip compression, reducing file size for storage and transfer ?

Example

import tarfile
import os

# Create sample files
with open("data1.txt", "w") as f:
    f.write("Sample data file 1")

with open("data2.txt", "w") as f:
    f.write("Sample data file 2")

# Create a compressed .tar.gz archive
with tarfile.open("TutorialsPoint.tar.gz", "w:gz") as tar:
    tar.add("data1.txt")
    tar.add("data2.txt")

print("Created TutorialsPoint.tar.gz successfully")

# Check file sizes for comparison
tar_size = os.path.getsize("TutorialsPoint.tar.gz")
print(f"Compressed archive size: {tar_size} bytes")
Created TutorialsPoint.tar.gz successfully
Compressed archive size: 184 bytes

Creating a Compressed .tar.bz2 File

For bzip2 compression, use the "w:bz2" mode. Bzip2 typically provides better compression ratios than gzip, making it ideal for archiving large datasets ?

Example

import tarfile

# Create sample files
with open("large_file.txt", "w") as f:
    f.write("This is a sample large file content " * 100)

# Create a compressed .tar.bz2 archive
with tarfile.open("TutorialsPoint.tar.bz2", "w:bz2") as tar:
    tar.add("large_file.txt")

print("Created TutorialsPoint.tar.bz2 successfully")

# Verify the archive
with tarfile.open("TutorialsPoint.tar.bz2", "r:bz2") as tar:
    print("Archive contents:", tar.getnames())
Created TutorialsPoint.tar.bz2 successfully
Archive contents: ['large_file.txt']

Comparison of Compression Methods

Compression Type File Extension Compression Ratio Speed Best For
None .tar None Fastest Quick archiving
Gzip .tar.gz Good Fast General use
Bzip2 .tar.bz2 Better Slower Maximum compression
XZ .tar.xz Best Slowest Long-term storage

Conclusion

Python's tarfile module makes it easy to create tar archives with or without compression. Use "w" mode for basic archiving, "w:gz" for general compression, and "w:bz2" for maximum compression when file size matters more than speed.

Updated on: 2026-03-24T18:41:29+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements