当前位置:网站首页>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
边栏推荐
- 六石认知学:大脑的显速与潜速
- Two months later, my second listing anniversary [June 2, 2022]
- Self made calculator (1 realized by Boolean logic operation unit and control unit programming)
- leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
- MySQL advanced learning notes
- 2022.6.12-----leetcode.890
- Summary of static memory allocation and dynamic memory allocation
- 即时配送的订单分配策略:从建模和优化-笔记
- Summary of interview questions
- 2022.6.12 - leetcode. 89.
猜你喜欢

Introduction to service grid and istio - continued

leetcode:6096. 咒语和药水的成功对数【排序 + 二分】

吃饭咯 干锅肥肠 + 掌中宝!

VirtualLab basic experiment tutorial -4 Single slit diffraction

GD32F4xx控制DGUS触控按键

论大型政策性银行贷后,如何数字化转型 ?-亿信华辰

Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language

从源码解析 MobX 响应式刷新机制

Pytest automated testing framework (II)

在思科模擬器Cisco Packet Tracer實現自反ACL
随机推荐
Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment
Review of MySQL (VII): use of tables
01 complexity
What is SAP support package stack
Virtual Lab Basic Experiment tutoriel - 4. Diffraction à fente unique
bilibili视频列表名字太长显示不全的解决方法
[Huawei cloud stack] [shelf presence] issue 10: difficulties and solutions of it monitoring and diagnosis in the cloud scenario of government enterprise hybrid in the cloud native Era
Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation
The difference between user status and system status in CRM
Gd32f4xx communicates with electric energy meter conforming to dlt645_ two
VirtualLab basic experiment tutorial -4 Single slit diffraction
Common troubleshooting tools and analysis artifacts are worth collecting
【矩阵论 & 图论】期末考试复习思维导图
【sql语句基础】——查(select)(单表查询)
国内如何下载ProxyStrike
C language learning -- data storage in memory
309. the best time to buy and sell stocks includes the freezing period
配送交付时间轻量级预估实践-笔记
国内如何下载Vega
Two months later, my second listing anniversary [June 2, 2022]