Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all files with directory path in given directory

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.

like image 218
gaurav Avatar asked Nov 24 '11 16:11

gaurav


People also ask

How do I extract all files in a directory?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How extract all files from a directory in Python?

To unzip a file in Python, use the ZipFile. extractall() method. The extractall() method takes a path, members, pwd as an argument and extracts all the contents.

Does os Listdir return full path?

Use os. listdir() function! We'll make a function for this, which simply gets the full path, and returns a list of all such names.

How to extract contents of documents from a specific directory?

It works just in the same way. First make sure that you create the specific directory that you want to extract into by using: Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory. Again repeating that you must create a separate directory before unpacking files:

How to get a list of all files in a directory?

os.listdir (): This method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then a list of files and directories in the current working directory will be returned.

How do I extract a file from a folder in Linux?

First make sure that you create the specific directory that you want to extract into by using: # mkdir -p /tmp/tgz Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory. # tar -zvxf documents.tgz -C /tmp/tgz/

How do I get the main directory path of a file?

Directory.GetFiles (“Your main directory path”,"*",searchOption.AllDirectories) The above solution absolutely works, just to add to the above.


1 Answers

Looks like you may have already found an answer, but here's my version anyway:

import sys, tarfile

def get_members(tar, prefix):
    if not prefix.endswith('/'):
        prefix += '/'
    offset = len(prefix)
    for tarinfo in tar.getmembers():
        if tarinfo.name.startswith(prefix):
            tarinfo.name = tarinfo.name[offset:]
            yield tarinfo

args = sys.argv[1:]

if len(args) > 1:
    tar = tarfile.open(args[0])
    path = args[2] if len(args) > 2 else '.'
    tar.extractall(path, get_members(tar, args[1]))
like image 191
ekhumoro Avatar answered Oct 02 '22 09:10

ekhumoro