Backports Tarfile

1.2.0 · active · verified Sat Mar 28

backports.tarfile provides a backport of the CPython standard library's `tarfile` module, allowing users on older Python versions (>=3.8) to access newer features and bug fixes related to tar archive handling. The current version is 1.2.0, released recently, with several updates in Spring 2024.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create and extract a gzipped tar archive using the backported `tarfile` module. The API is identical to the standard library's `tarfile`.

import os
from backports import tarfile

# Create a dummy file
with open('example.txt', 'w') as f:
    f.write('Hello, backported tarfile!')

# Create a tar.gz archive
with tarfile.open('example.tar.gz', 'w:gz') as tar:
    tar.add('example.txt')

print('Archive created: example.tar.gz')

# Extract the archive
with tarfile.open('example.tar.gz', 'r:gz') as tar:
    tar.extractall(path='./extracted_archive')

print('Archive extracted to ./extracted_archive')

# Clean up
os.remove('example.txt')
os.remove('example.tar.gz')
os.remove('./extracted_archive/example.txt')
os.rmdir('./extracted_archive')

view raw JSON →