Code for How to Compress and Decompress Files in Python Tutorial

View on Github

import tarfile
from tqdm import tqdm # pip3 install tqdm

def decompress(tar_file, path, members=None):
    Extracts `tar_file` and puts the `members` to `path`.
    If members is None, all members on `tar_file` will be extracted.
    tar =, mode="r:gz")
    if members is None:
        members = tar.getmembers()
    # with progress bar
    # set the progress bar
    progress = tqdm(members)
    for member in progress:
        tar.extract(member, path=path)
        # set the progress description of the progress bar
        progress.set_description(f"Extracting {}")
    # or use this
    # tar.extractall(members=members, path=path)
    # close the file

def compress(tar_file, members):
    Adds files (`members`) to a tar_file and compress it
    # open file for gzip compressed writing
    tar =, mode="w:gz")
    # with progress bar
    # set the progress bar
    progress = tqdm(members)
    for member in progress:
        # add file/folder/link to the tar file (compress)
        # set the progress description of the progress bar
        progress.set_description(f"Compressing {member}")
    # close the file

# compress("compressed.tar.gz", ["test.txt", "test_folder"])
# decompress("compressed.tar.gz", "extracted")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="TAR file compression/decompression using GZIP.")
    parser.add_argument("method", help="What to do, either 'compress' or 'decompress'")
    parser.add_argument("-t", "--tarfile", help="TAR file to compress/decompress, if it isn't specified for compression, the new TAR file will be named after the first file to compress.")
    parser.add_argument("-p", "--path", help="The folder to compress into, this is only for decompression. Default is '.' (the current directory)", default="")
    parser.add_argument("-f", "--files", help="File(s),Folder(s),Link(s) to compress/decompress separated by ','.")

    args = parser.parse_args()
    method = args.method
    tar_file = args.tarfile
    path = args.path
    files = args.files

    # split by ',' to convert into a list
    files = files.split(",") if isinstance(files, str) else None

    if method.lower() == "compress":
        if not files:
            print("Files to compress not provided, exiting...")
        elif not tar_file:
            # take the name of the first file
            tar_file = f"{files[0]}.tar.gz"
        compress(tar_file, files)
    elif method.lower() == "decompress":
        if not tar_file:
            print("TAR file to decompress is not provided, nothing to do, exiting...")
        decompress(tar_file, path, files)
        print("Method not known, please use 'compress/decompress'.")