Extract all files with directory path in given directory

Posted by gaurav on Stack Overflow See other posts from Stack Overflow or by gaurav
Published on 2011-11-24T16:04:09Z Indexed on 2011/11/24 17:52 UTC
Read the original article Hit count: 222

Filed under:
|
|

I have a tar archive in which I have a directory which I need to extract in a given directory. For example: I have a directory

TarPrefix/x/y/z

in a tar archive I want to extract it in a given target directory for example: extracted/a/ this directory should contain all the files and directories contained in directory TarPrefix/x/y/z.

subdir_and_files = [  tarinfo for tarinfo in tar.getmembers()
                      if tarinfo.name.startswith("subfolder/")
                   ]

to get the list of all the members in the directory path "subfolder/" and then I extract it using tar.extractall(extracted/a,subdir_and_files) but it extracts all the members with their directory path For example this results in extracted/a/x/y/z. Could you please help me in extracting these files in the given folder.

© Stack Overflow or respective owner

Related posts about python

Related posts about extract