tarfile - Adding files from different drives
<p>I am trying to archive and compress multiple directories spread along multiple drives using the tarfile library.
The problem is that tarfile merges the paths even if two files are stored in different drives.
For example:</p>

<pre><code>import tarfile
with tarfile.open(r"D:\Temp\archive.tar.gz", "w:gz") as tf:
    tf.add(r"C:\files\foo")
    tf.add(r"D:\files\bar")
</code></pre>

<p>Will create an archive containing the following files:</p>

<pre><code>archive.tar.gz
|
    files
    |
        foo
        bar
</code></pre>

<p>Is there a way of creating this:</p>

<pre><code>archive.tar.gz
|
    C
    |
        files
        |
            foo
    D
    |
        files
        |
            bar
</code></pre>

<p>Thanks in advance.</p>

<p>You'll need to use <code>tarfile.addfile()</code> instead of <code>tarfile.add()</code> :</p>

<p>With TarInfo you can specify the filename which will be used in the archive.</p>

<p>Exemple :</p>

<pre><code>with open(r"C:\files\foo", "rb") as ff:
    ti = tf.gettarinfo(arcname="C/files/foo", fileobj=ff)
    tf.addfile(ti, ff)
</code></pre>

<p>Or maybe, a faster solution :</p>

<pre><code>tf.add('/path/to/dir/to/add/', arcname='C/files')
tf.add('/path/to/otherdir/to/add/', arcname='D/files')
</code></pre>

python gzip tar tarfile