Python tarfile module with Examples

Python tarfile module is used to read and write tar archives. Python provides us excellent tools and modules to manage compressed files, which includes (but not limited to) performing file and directory compression with different mechanisms like gzip, bz2 and lzma compression.

In this post, we will see various practical demonstrations of Python tarfile module functions. This is similar to python zip function. Let’s get started.

Python tarfile module

Python tarfile module provides us functions to perform various operations like:

  • read and write gzip, bz2 and lzma archives
  • read and write POSIX.1-1988 (ustar) format
  • read and write support for GNU tar format
  • read and write gzip, bz2 and lzma archives

Apart from these features, we can also handle directories and restore file information like timestamp, access permissions and owner.

Checking validity of TAR files

We will start by a simplest example of checking if a file is a valid TAR file. We will use is_tarfile() function to do this:

Let’s run this example and check the output:
python tarfile read metadata
Note that these files should exist in the directory you run this script in.

Reading TAR file metadata

In this section, we will study metadata related to a TAR file like what files does it contain, using the open() and getnames() function:

Let’s run this example and check the output:
python tarfile read metadata
Note that, we just put sample files in this TAR to demonstrate.

Let’s get a little deep in getting the file’s metadata before moving on to next example. We will print its size and much more information related to it:

When we run this program, we can see much more information related to the files:

python tarfile append files

Here, we will extract files from the archive file:

Let’s run this example and check the output:

python tarfile append files

Adding Files to an Archive

Here, we will add files to an archive file:

Let’s run this example and check the output:
python tarfile append files
Here, it is worth noticing that ‘w’ doesnn’t preserve previous contents of the file. We can instead use ‘a’ mode to append files to an archive.

Appending Files to an Archive

Here, we will append files to an archive file, instead of using the ‘w’ mode:

Let’s run this example and check the output:
python tarfile append files
Clearly, after adding README.txt to the TAR, there now exists 2 files.

Reference: API Documentation.

By admin

Leave a Reply