import tarfile import pathlib path = pathlib.Path('test.tgz') with tarfile.open(path, 'w|gz') as tar: tar.add('.')