Skip to content

[Python] Use "tarfile" Module To Compress Or Decompress Files

tarfile is a built-in module in Python, it can operate some file formats such as gzip, bz2, lzma... to compressed or decompressed.

The extensions of file that can be operated are tar.gz, tar.bz2, tar.xz.

In the following, first introduce the several operating modes listed in the official documents (Links are linked at the end of the article)


open()

ModeOperation
'r' or 'r:*'Open for reading with transparent compression (recommended).
'r:'Open for reading exclusively without compression.
'r:gz'Open for reading with gzip compression.
r:bz2'Open for reading with bzip2 compression.
'r:xz'Open for reading with lzma compression.
'x' or 'x:'Create a tarfile exclusively without compression. Raise an FileExistsError exception if it already exists.
'x:gz'Create a tarfile with gzip compression. Raise an FileExistsError exception if it already exists.
'x:bz2'Create a tarfile with bzip2 compression. Raise an FileExistsError exception if it already exists.
'x:xz'Create a tarfile with lzma compression. Raise an FileExistsError exception if it already exists.
'a' or 'a:'Open for appending with no compression. The file is created if it does not exist.
'w' or 'w:'Open for uncompressed writing.
'w:gz'Open for gzip compressed writing.
'w:bz2'Open for bzip2 compressed writing.
'w:xz'Open for lzma compressed writing.
https://docs.python.org/3/library/tarfile.html

The above are the different modes of operation in tarfile.


Sample Code

In Python, the usage of tarfile and zipfile are very similar.

First, suppose I have to compress a folder named test/, it is a really folder in my computer.

There are two files test_01.txt and test_02.txt under the folder


Use tarfile to compress files (take tar.gz as an example)

# coding: utf-8
import os
import tarfile


# tarfile example
def tar_dir(path):
    tar = tarfile.open('test.tar.gz', 'w:gz')
   
    for root, dirs, files in os.walk(path):
        for file_name in files:
            tar.add(os.path.join(root, file_name))

    tar.close()


if __name__ == '__main__':
    path = 'test'
    tar_dir(path)



Use tarfile to decompress the archive (take tar.gz as an example)

# coding: utf-8
import os
import tarfile


# tarfile example
def tar_extract(file_path):
    tar = tarfile.open(file_path, 'r:gz')
    tar.extractall()


if __name__ == '__main__':
    file_path = 'test.tar.gz'
    tar_extract(file_path)



References


References

Leave a Reply