CodeMed
CodeMed

Reputation: 9201

Removing excess path data from inside of tar.gz file

A text file contains a dynamically-written path to a directory. The contents of that directory must be archived into a new tar.gz file. Specifically, the directory whose contents must be archived will have an OS-specific path in the form of "C:\path\to\someDir" or "/path/to/someDir", and the path to the directory will be stored in a variable named otherDir. The contents of otherDir are what needs to be stored, not the "C:\path\to\someDir" or "/path/to/someDir".

I tried the below code:

otherDir = "C:\path\to\someDir"  # This is populated dynamically from a text file.
if os.path.exists(otherDir):
    import tarfile
    EXCLUDE_FILES = ['unwantedSubdirectory']
    tar = tarfile.open("my.tar.gz", "w:gz")
    tar.add(otherDir, filter=lambda x: None if x.name in EXCLUDE_FILES else x)
    tar.close()

The problem is that the code is producing a my.tar.gz whose contents all begin with either "C:\path\to\someDir" or "/path/to/someDir". The contents of the resulting my.tar.gz are otherwise correct.

How must the code be changed in order to remove the "C:\path\to\someDir" or "/path/to/someDir" prefix from the name of each member of the resulting my.tar.gz file, while retaining the rest of the data in the file?

Upvotes: -2

Views: 41

Answers (0)

Related Questions