March 6, 2021

How to create a compressed tar file with the relative file path in python

By chandan

Whether you are working on file-heavy applications or on logs heavy applications, compressing files to save storage is very useful for efficient files management as well as disk storage management.

Recently I worked on a project which required compressing a set of files and folders and below are some useful things I noticed while creating a compressed tar file in python:

Create a simple tar file from a given path-

import tarfile

os.chdir(tar_location)
with tarfile.open(tar_location + '.tar', mode='w:tar') as browser_tar:
    browser_tar.add(tar_location)

so here, first, we move to the input directory after that, by using tarfile we add all the content of a folder in the tar file, simple enough right?

But there was a problem in this method, it creates a tar with absolute path i.e when you untar file then folder structure starts from input location, something like c://users/chandan/tarfilelocation.

Ideally when you want to send this tar as email content (like in the case of build logs) or send it to an application to start the processing of files(application-specific), then after creating a tar file, the structure of the folder should start from the relative folder location i.e. ./tarfilelocation in this case.

To see the whole path of your tar during debugging you can make use of getmembers() method which gives the info about the whole tar file path is shown below:

browser_tar.getmembers()

Now, to solve this problem tarfile module provides an extra argument called “arcname” and using it you can easily tar file with relative path without worrying about user directories as shown below:

browser_tar.add(tar_location, arcname=".")

and not only that, by using arcname, you can also provide a specific name too which could be very useful where you are playing with multiple tar files and you need to append something like a unique timestamp to create tar files with a unique name as shown below:

browser_tar.add(tar_location, arcname=unique_tar_name)


In this way, you can easily create compressed tar files and efficiently manage your disk space and set of files, do you have something to add then do leave your comment in the comments section.