PDA

View Full Version : [SOLVED] Python tarfile.add adds all folders of specified path?



kumoshk
March 4th, 2011, 07:41 AM
I'm working with Python's tarfile module. From an arbitrary location, I need to be able to compress a directory and its contents in another arbitrary location. This works and all, but I'm running into the following problem:

Instead of just adding the folder and everything inside it (preserving only the structure within) it adds all the folders in the path that lead up to the folder I want to add!

Here's a simple example demonstrating what I'm doing:


import tarfile

tar = tarfile.open("myArchive.tar", "w")
tar.add("foo/bar")
tar.close()

What I want is a tar file that merely contains the 'bar' folder and all its contents (folders and all). However, what this does is add a folder called 'foo' containing the 'bar' folder with the 'bar' folder containing all its contents. In my current project, my path is a lot longer—so this is a lot more problematic.

Any ideas on how to fix this without a lot of overhead?

Thanks!

kumoshk
March 4th, 2011, 08:00 AM
I found a solution. Do this instead:


import tarfile

tar = tarfile.open("myArchive.tar", "w")
tar.add("foo/bar", arcname="bar")
tar.close()

So, setting arcname is the solution (I don't know if it's the intended one, though, but I'm guessing so).