Question

I am writing a program in python and using tarfile to extract tarfiles. Some of these tarfiles contain folders which start with a / or (Alternatively for windows \) which cause problems (files are extracted to wrong place). How can I get around this issue and make sure that the extraction ends up in correct place ?

Was it helpful?

Solution

The docs for tarfile explicitly warn about such a scenario. Instead you need to iterate over the content of the tar file and extract each file individually:

import os
import tarfile

extract_to = "."
tfile = tarfile.open('so.tar')

members = tfile.getmembers()
for m in members:
    if m.name[0] == os.sep:
        m.name = m.name[1:]
    tfile.extract(m, path=extract_to)

OTHER TIPS

Did you try extractall() method? As I remeber one of the this method arguments contains information where archive should be extracted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top