Skip to content Skip to sidebar Skip to footer

How To Compress A Tar File In A Tar.gz Without Directory?

I'm looking for a way to compress a tar file in a tar.gz without directory. Today my code generate a TAR file without directory with 'tarfile' library and arcname arguments but whe

Solution 1:

For individual file(s):

tar.add(file, arcname=os.path.basename(file))

for each file that you want to add. basename will strip the directory information.

Or, for a recursive directory:

def flatten(tarinfo):
    tarinfo.name = os.path.basename(tarinfo.name)
    return tarinfo

tar = tarfile.open("example.tar.gz", "w:gz")
tar.add("directory", filter=flatten)
tar.close()

Post a Comment for "How To Compress A Tar File In A Tar.gz Without Directory?"