tarfile | Python Standard Library – Real Python
The Python tarfile module provides a powerful and flexible way to read and write tar archives. It supports both compressed and uncompressed tar files and allows for easy manipulation of archive contents.
Here’s a quick example:
Key Features
- Supports reading and writing of tar archives
- Handles both compressed (gzip, bzip2, lzma) and uncompressed tar files
- Provides random access to archive members
- Allows for adding and extracting files from tar archives
- Works with both files on disk and file-like objects—including in-memory archives
- Enables inspection and modification of file metadata
Frequently Used Classes and Functions
Examples
Open and list the contents of a tar archive:
Extract a specific file from a tar archive:
Create a new tar archive and add files to it:
Common Use Cases
- Archiving and compressing multiple files or directories into a single tar archive
- Extracting specific files or all contents from tar archives
- Inspecting archive contents, including file metadata, without extraction
- Selectively extracting or adding files based on patterns or criteria
- Creating automated backups of directories or projects
- Working with archives entirely in memory
- Preserving or modifying file permissions, timestamps, and ownership
- Automating deployment, packaging, or continuous integration tasks
Real-World Example
Suppose you want to back up a directory of text files into a compressed tar archive using a function to perform the backup. You can use the tarfile module to accomplish this task efficiently:
In this example, you create a function that collects all .txt files from the specified directory, creates a gzip-compressed tar archive containing those files, and returns a Path object representing the archive.
For additional information on related topics, take a look at the following resources: