import os, tarfile def compress_file(src_tar_filename, des_filename): """ :param scr_tar_filename: :param des_filename: :return: """ files = os.walk(src_tar_filename) with tarfile.open(name=des_filename, mode='w|gz') as t: for root, dir, file in files: for name in file: t.add(os.path.join(root, name)) def extract_gz(src_tar_filename, des_filename): """ :param src_tar_filename: :param des_filname: :return: """ with tarfile.open(src_tar_filename) as t: t.extractall(des_filename)