1

I am trying to extract specific folders which I am interested in from big tar file.

import  tarfile
import os

list_dir = []
models = ["usb","test"]
with tarfile.open("build_today.tar.gz") as tar:
for tarinfo in tar.getmembers():
    if tarinfo.name.startswith(os.path.join("build_today","pips")):
        for model in models:
            if tarinfo.name.startswith(os.path.join("build_today","pips",model,"")):
                list_dir.append(tarinfo.name)
    elif tarinfo.name.startswith(os.path.join("build_today","objects")):
        for model in models:
            if tarinfo.name.startswith(os.path.join("build_today","objects",model,"")):
                list_dir.append(tarinfo.name)
    else:
        list_dir.append(tarinfo.name)
print list_dir
tar.extractall(members=list_dir)

I am able to get print 'list_dir', Where i can see the content that i am expecting. But it failes while extracting.

It fails with Error:

Traceback (most recent call last):
File "tar.py", line 18, in <module>
tar.extractall(members=list_dir)
File "/grid/common/pkgs/python/v2.7.10/lib/python2.7/tarfile.py", line 2067, in extractall
if tarinfo.isdir():
AttributeError: 'str' object has no attribute 'isdir'

Please help!

1
  • please format your code to comply with PEP-8, and use line numbers in the listing. Commented Sep 25, 2019 at 6:45

1 Answer 1

1

From a first analysis, seems that the problem is here:

list_dir.append(tarinfo.name)

Here you are appending the file name as a string. Instead, you need the "file object" that have the.isdir() builtin method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.