Balint Balint - 1 year ago 105
Python Question

Python tarfile: how to use tar+gzip compression with follow symbolic link?

How could I use tar+gzip compression with "follow symbolic link" feature in Python 3.4? The problem is:

  • supports "w:gz" mode but does not support "dereference" option

  • tarfile.tarfile() supports "dereference" but does not support "w:gz" mode


mode = ""
if bckentry['method'] == "tar":
mode = "w"
elif bckentry['method'] == "targz":
mode = "w:gz"

archive = tarfile.TarFile(name=filepath, mode=mode)
archive.dereference = True if bckentry['followsym'] == "yes" else False
# archive =, mode=mode)

if bckentry['withpath'] == 'yes':
for entry in bckentry['include_dirs']:
archive.add(entry, filter=self.filter_tar)
elif bckentry['withpath'] == 'no':
for entry in bckentry['include_dirs']:
archive.add(entry, arcname=os.path.basename(entry), filter=self.filter_tar)

Answer Source is a shortcut for the classmethod, that in turn calls the TarFile constructor. The documentation is a bit vague, but from the code it is apparent that the first two will pass the dereference keyword argument and all other unconsumed kwargs to the TarFile constructor.

Thus you can use dereference with any of them, if you just pass it as a keyword argument:

archive ='foo.tar.gz', mode='w:gz', dereference=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download