Python tarfile: how to use tar+gzip compression with follow symbolic link?
<p>How could I use tar+gzip compression with "follow symbolic link" feature in Python 3.4? The problem is:</p>

<ul>
<li>tarfile.open() supports "w:gz" mode but does not support "dereference" option</li>
<li>tarfile.tarfile() supports "dereference" but does not support "w:gz" mode</li>
</ul>

<p>Code:</p>

<pre><code>...
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 = tarfile.open(filepath, 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)
...
</code></pre>

<p><a href="https://docs.python.org/3/library/tarfile.html#tarfile.open" rel="nofollow"><code>tarfile.open</code></a> is a shortcut for the <a href="https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.open" rel="nofollow"><code>TarFile.open</code></a> classmethod, that in turn calls the <a href="https://docs.python.org/3/library/tarfile.html#tarfile-objects" rel="nofollow"><code>TarFile</code></a> constructor. The documentation is a bit vague, but from the code it is apparent that the first two will pass the <code>dereference</code> keyword argument and all other unconsumed kwargs to the <code>TarFile</code> constructor. </p>

<p>Thus you can use <code>dereference</code> with any of them, if you just pass it as a keyword argument:</p>

<pre><code>archive = tarfile.open(name='foo.tar.gz', mode='w:gz', dereference=True)
</code></pre>

python gzip tar dereference tarfile