Cemre Mengü
Cemre Mengü

Reputation: 18754

Extracting a tar file with folders starting with /

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 ?

Upvotes: 2

Views: 182

Answers (2)

John Keyes
John Keyes

Reputation: 5604

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)

Upvotes: 2

tasmoz
tasmoz

Reputation: 1

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

Upvotes: 0

Related Questions