Results 1 to 2 of 2

Thread: Python tarfile.add adds all folders of specified path?

  1. #1
    Join Date
    Apr 2008
    Beans
    315
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Python tarfile.add adds all folders of specified path?

    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:

    Code:
    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!
    Last edited by kumoshk; March 4th, 2011 at 07:44 AM.

  2. #2
    Join Date
    Apr 2008
    Beans
    315
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Re: Python tarfile.add adds all folders of specified path?

    I found a solution. Do this instead:

    Code:
    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).

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •