Python 2.7 extract a single file from tar.xz

301 Views Asked by At

I need to extract a single file and copy it to some other location using python.

I have this code which does the required thing for gzip compressed data,

tar = tarfile.open(tar_filename)

for member in tar.getmembers():
        if "abc" in member.name:
            member.name = os.path.basename(member.name)
            tar.extract(member, "/tmp/abc")
            return True

I want to do the same thing i.e extract a file in the XZ compressed data. I cant use tarfile.open because it doesn't work. I found that we can open the file using lzma but couldnt find anything helpful that does the similar functionality.

I need to use python 2.7

Do you think we can do the same thing with for xz compressed as well? if not can you suggest me a way to do it.

Thanks

0

There are 0 best solutions below