tarfile
Updated March 13, 2026 · Modules
archives compression stdlib files
The tarfile module reads and writes tar archives in various compression formats. It works with uncompressed tar files as well as gzip-compressed (.tar.gz), bzip2-compressed (.tar.bz2), and xz-compressed (.tar.xz) archives.
Opening Archives
Reading an existing archive
import tarfile
# Open for reading
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
# List all members
for member in tar.getmembers():
print(member.name, member.size, member.mode)
Creating a new archive
import tarfile
# Create a new gzip-compressed archive
with tarfile.open('backup.tar.gz', 'w:gz') as tar:
# Add files by name
tar.add('file.txt')
tar.add('folder/', arcname='folder')
# Or add from memory
import io
data = b'Hello, world!'
info = tarfile.TarInfo(name='hello.txt')
info.size = len(data)
tar.addfile(info, io.BytesIO(data))
Extracting Archives
Extract all files
import tarfile
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
tar.extractall(path='destination_folder')
Extract specific files
import tarfile
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
# Extract a single file
tar.extract('myfile.txt', path='destination')
# Extract multiple files
members = [tar.getmember('file1.txt'), tar.getmember('file2.txt')]
tar.extractall(path='destination', members=members)
Working with Members
Get member information
import tarfile
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
# Get a specific member
info = tar.getmember('path/to/file.txt')
print(info.name)
print(info.size)
print(info.mtime) # modification time
print(info.mode) # file permissions
print(info.isfile())
print(info.isdir())
Read file contents without extracting
import tarfile
with tarfile.open('archive.tar.gz', 'r:gz') as tar:
# Get the TarInfo object
member = tar.getmember('readme.txt')
# Extract file object to read in memory
f = tar.extractfile(member)
content = f.read()
print(content.decode('utf-8'))
Writing Archives
Add files with custom permissions
import tarfile
import os
with tarfile.open('secure.tar', 'w') as tar:
# Add a file with specific permissions
info = tarfile.TarInfo('sensitive.dat')
info.mode = 0o600 # owner read/write only
info.uid = os.getuid()
info.gid = os.getgid()
with open('data.bin', 'rb') as f:
tar.addfile(info, f)
Create archives with different compression
import tarfile
# Various compression modes
modes = {
'': 'uncompressed',
'r:gz': 'gzip compressed',
'r:bz2': 'bzip2 compressed',
'r:xz': 'xz compressed',
'r:*': 'auto-detect compression',
}
for mode, desc in modes.items():
ext = 'tar'
if 'gz' in mode:
ext = 'tar.gz'
elif 'bz2' in mode:
ext = 'tar.bz2'
filename = f'archive.{ext}'
with tarfile.open(filename, f'w{mode}' if mode else 'w') as tar:
tar.add('myfile.txt')
print(f'Created {desc} archive')
Common Patterns
Backup a directory
import tarfile
from datetime import datetime
import os
def create_backup(source_dir, backup_name=None):
if backup_name is None:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_name = f'backup_{timestamp}.tar.gz'
with tarfile.open(backup_name, 'w:gz') as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
return backup_name
List archive contents without extracting
import tarfile
def list_contents(archive_path):
with tarfile.open(archive_path, 'r:*') as tar:
print(f'Total members: {len(tar.getmembers())}')
for member in tar:
prefix = 'd' if member.isdir() else 'f'
print(f'{prefix} {member.name} ({member.size} bytes)')
Verify archive integrity
import tarfile
def verify_archive(archive_path):
try:
with tarfile.open(archive_path, 'r:*') as tar:
# Try to read all members
for member in tar:
if member.isfile():
f = tar.extractfile(member)
f.read()
return True
except Exception as e:
print(f'Archive is corrupted: {e}')
return False