tarfile module in python

Master Tarfile Module in Python | 5 mins read

The tarfile module in Python provides a convenient way to work with tar files in Python. With the tarfile module, you can create, open, and extract tar files, as well as list the contents of a tar file and get information about the files and directories it contains.

Table of Contents

83 / 100

Table of Contents


Good to Go

Hello, and welcome back to another exciting tutorial.Today, we are going to talk about “Handling tar files in Python“. This tutorial covers:-

  • Meaning of tar file
  • Advantages of tar file
  • Creating a tar file
  • Extracting the content of a tar file
  • Code snippets for above topics for better understanding

If you aren’t following along, I would recommend you to go back where you left or start from here.

What is a tarfile?

Back to basics

A tar file is a type of archive file that stores multiple files, directories, and other data in a single location. Tar files are commonly used for storing and sharing collections of files, such as documents, photos, or music. . Tar files can be created and extracted using a variety of different tools, including built-in tools on most Unix-like operating systems and specialized tar file software.

Tar files are often referred to as tarballs, due to their traditional use of the .tar.gz or .tar.bz2 filename extensions, which indicate that the tar file is compressed using the gzip or bzip2 algorithms, respectively.

  1. Compression: Tar files can be compressed, which makes them take up less space and makes them faster to transfer.

  2. Convenience: It is easier to manage and transport a single tar file than a large number of individual files and directories.

  3. Preserves file structure: A tar file preserves the file and directory structure of the original files, so when the tar file is uncompressed, the original directory structure is recreated.

  4. Cross-platform compatibility: Tar files can be extracted on any platform that has a tar utility, which makes them a convenient way to package files for transfer between different operating systems.

tarfile module


First, you’ll need to import the tarfile module into your Python script. You can do this by using the import statement, like this:

import tarfile

Once you’ve imported the tarfile module, you can start working with tar files.

Creating a Tar file

Archive File

To create a tar file, you’ll first need to create a TarFile object by calling the tarfile.open() method. This method takes the name of the tar file you want to create and the mode in which you want to open the file as arguments.

For example, if you want to create a tar file named my_tar_file.tar, you would use the following code:

tar = tarfile.open("my_tar_file.tar", "w")

The tarfile.open() method also takes an optional mode argument, which specifies the mode in which the tar file should be opened. In the example above, we use the “w” mode, which indicates that we want to open the tar file for writing.

Once you’ve created the TarFile object, you can add files to the tar file by using the add() method. This method takes the name of the file you want to add to the tar file as an argument.

For example, if you want to add a file named my_file.txt to the tar file, you could use the following code:


You can also use the add() method to add directories to the tar file. This will include the files and subdirectories within the directory, recursively.

For example, if you want to add a directory named my_directory to the tar file, you could use the following code:


In addition to adding files and directories to the tar file, you can also add files and directories with different name in the tar file.
To do this, you can use the add() method in combination with the arcname parameter, which specifies the name of the file or directory within the tar file.

For example, if you want to add a file named my_file.txt to the tar file and give it the name my_new_file.txt within the tar file, you could use the following code:

tar.add("my_file.txt", arcname="my_new_file.txt")
Once you’ve added all of the files and directories you want to the tar file, you can close the tar file by using the close() method, like this:

Extracting content of a tar file

Unarchive File

To unarchive a tar file in Python, you can use the tarfile module. Here is an example of how to extract the contents of a tar file:
import tarfile
# Open the tar file
tar = tarfile.open("file.tar", "r")
# Extract all the contents of the tar file
This will extract the contents of the tar file file.tar in the current directory.
If you want to extract the contents of the tar file to a specific directory, you can use the extractall() method and pass the path to the destination directory as an argument, like this:

You can also extract individual files or directories from the tar file by specifying their names.

For example:

# Extract a single file
#Extract a directory and its contents

Finally, you can use the getnames() method to get a list of the names of all the files and directories in the tar file, and the getmember() method to get information about a specific file or directory in the tar file.


Overall, the tarfile module is a useful tool for working with tar files in Python, and it is an important part of the Python standard library. Whether you need to create a tar file for data backup or archive, or extract the contents of an existing tar file, the tarfile module can help you get the job done.