Priya Shah
Priya Shah

Reputation: 9

tar: Removing leading `/' from member names showing in tarfile.add (python)

Getting the following warning while creating .tar file of a folder that consists of folders and files:

"tar: Removing leading `/' from member names"

Tarfile is created with the following code snippet.I have sample folder in my current directory which I am using to add files in tar file But, I want to get rid of the above warning:

def create_tarball(self, dir_name):    
        try:
           with tarfile.open("example.tar", "w:tar") as tar:
                    tar.add("./sample/"+dir_name, arcname=os.path.basename(".sample/"))
        except Exception as e:
                raise(e)

I have seen the solutions for the command line like using -C. But how can I implement it in this python code?

Upvotes: 0

Views: 217

Answers (1)

Priya Shah
Priya Shah

Reputation: 9

Solution: Adding / to "./sample/" in os.path.basename param. Code snippet after update:

def create_tarball(self, dir_name):    
        try:
           with tarfile.open("example.tar", "w:tar") as tar:
                    tar.add("./sample/"+dir_name, arcname=os.path.basename("./sample/"))
        except Exception as e:
                raise(e)

Upvotes: 0

Related Questions