Extract only one directory from tar

I am working on a project in python in which I need to extract only the subfolder of the tar archive of not all files. I tried to use

tar = tarfile.open(tarfile) tar.extract("dirname", targetdir) 

But this does not work, it does not extract the given subdirectory, and the exception is not thrown. I am new to python. Also, if the above function does not work for directories, then what is the difference between this command and tar.extractfile ()?

+6
source share
2 answers

Based on the second example from the tarfile module documentation , you can extract the contained subfolder and all its contents with something like this

 with tarfile.open("sample.tar") as tar: subdir_and_files = [ tarinfo for tarinfo in tar.getmembers() if tarinfo.name.startswith("subfolder/") ] tar.extractall(members=subdir_and_files) 

Creates a list of the subfolder and its contents, and then uses the recommended extractall() method to extract only them. Of course, replace "subfolder/" with the actual path (relative to the root of the tar file) of the subfolder you want to extract.

+11
source

Another answer will save the subfolder path, which means that subfolder/a/b will be extracted before ./subfolder/a/b . To extract the subfolder to the root directory, so subfolder/a/b will be extracted to ./a/b , you can rewrite the paths as follows:

 def members(tf): l = len("subfolder/") for member in tf.getmembers(): if member.path.startswith("subfolder/"): member.path = member.path[l:] yield member with tarfile.open("sample.tar") as tar: tar.extractall(members=members(tar)) 
+4
source

Source: https://habr.com/ru/post/900773/


All Articles