当前位置:网站首页>Tarfile decompress nested tar

Tarfile decompress nested tar

2022-06-12 18:42:00 jjw_ whz

The directory structure to be unzipped and the directory structure to be unzipped are as follows :

ILSVRC2012_img_train.tar
    n01440764.tar
        n01440764_18.JPEG
        n01440764_36.JPEG
        ...
    n01443537.tar
        n01443537_2.JPEG
        ...
    n01484850.tar
        n01484850_17.JPEG
        ...
    ...
 You want to decompress it to the following effect :
ILSVRC2012_img_train  Folder 
    n01440764         Folder 
        n01440764_18.JPEG
        n01440764_36.JPEG
        ...
    n01443537         Folder 
        n01443537_2.JPEG
        ...
    n01484850         Folder 
        n01484850_17.JPEG
        ...
    ...

Unzip the code as follows :

import os
import tarfile

file_path = './ILSVRC2012_img_train.tar'
file_name = file_path.split('/')[-1].split('.tar')[0]
os.mkdir(file_name) #  Generate ILSVRC2012_img_train Folder 
tar = tarfile.open(file_path)
tar.extractall(file_name) #  Layer 1 decompression , Will be right ILSVRC2012_img_train.tar unpack 
sub_tar_file_name_list = tar.getnames() #  obtain ILSVRC2012_img_train.tar Subdirectories in 
sub_file_name_list = []
for sub_tar_file_name in sub_tar_file_name_list:
    sub_file_name_list.append(sub_tar_file_name.split('.tar')[0])
for dir_name in sub_file_name_list:
    os.mkdir(file_name+'/'+dir_name) #  Generate without .tar The catalog of 
tar.close()

for extract_name in sub_tar_file_name_list:
    sub_tar = tarfile.open('./'+file_name+'/'+extract_name)
    sub_tar.extractall('./'+file_name+'/'+extract_name.split('.tar')[0])  #  The second decompression  
    sub_tar.close()
    os.system('rm -rf '+'./'+file_name+'/'+extract_name) #  Delete the layer 2 tape after decompressing the layer 1 .tar The file of 
# os.system('rm -rf file_path)  If you want to ILSVRC2012_img_train.tar Also deleted, just use this 
原网站

版权声明
本文为[jjw_ whz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121836375852.html