当前位置:网站首页>tarfile解压嵌套tar
tarfile解压嵌套tar
2022-06-12 18:36:00 【jjw_whz】
要解压的目录结构和要解压成的目录结构如下:
ILSVRC2012_img_train.tar
n01440764.tar
n01440764_18.JPEG
n01440764_36.JPEG
...
n01443537.tar
n01443537_2.JPEG
...
n01484850.tar
n01484850_17.JPEG
...
...
想解压成如下效果:
ILSVRC2012_img_train 文件夹
n01440764 文件夹
n01440764_18.JPEG
n01440764_36.JPEG
...
n01443537 文件夹
n01443537_2.JPEG
...
n01484850 文件夹
n01484850_17.JPEG
...
...
解压代码如下:
import os
import tarfile
file_path = './ILSVRC2012_img_train.tar'
file_name = file_path.split('/')[-1].split('.tar')[0]
os.mkdir(file_name) # 生成ILSVRC2012_img_train文件夹
tar = tarfile.open(file_path)
tar.extractall(file_name) # 第一层解压,将对ILSVRC2012_img_train.tar进行解压
sub_tar_file_name_list = tar.getnames() # 获取ILSVRC2012_img_train.tar中的子目录
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) # 生成不带.tar的目录
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]) # 第二次解压
sub_tar.close()
os.system('rm -rf '+'./'+file_name+'/'+extract_name) # 删除第一层解压后的第二层带.tar的文件
# os.system('rm -rf file_path) 如果想将ILSVRC2012_img_train.tar也删除了就使用这个
边栏推荐
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- MySQL advanced learning notes
- OpenGL shadow implementation (hard shadow)
- VirtualLab basic experiment tutorial -4 Single slit diffraction
- General differences between SQL server versions released by Microsoft in different periods so far, for reference
- Getting started with the go language is simple: read / write lock
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- HTTP cache < strong cache and negotiation cache >
- JS moves the 0 in the array to the end
- 配送交付时间轻量级预估实践-笔记
猜你喜欢
Common methods and examples of defect detection based on Halcon
迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of swimming fins in the global market in 2022
CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
Write a select based concurrent server
Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update
Review of MySQL (4): sorting operation
Two months later, my second listing anniversary [June 2, 2022]
Virtual Lab Basic Experiment tutoriel - 4. Diffraction à fente unique
随机推荐
General differences between SQL server versions released by Microsoft in different periods so far, for reference
Enhanced version of unit test code displayed by SAP e-commerce cloud Spartacus UI checkout spinner
网盘和对象云存储管理之磁盘映射工具比较
Why my order by create_ Time ASC becomes order by ASC
Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
MySQL数据库(28):变量 variables
基于Halcon的矩形卡片【手动绘制ROI】的自由测量
Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
机器学习在美团配送系统的实践:用技术还原真实世界-笔记
VirtualLab basic experiment tutorial -6 Blazed grating
VirtualLab基础实验教程-6.闪耀光栅
Review of MySQL (VI): usage of Union and limit
Random talk about redis source code 91
Leetcode topic [string] -151- flip words in string
Vue —— 进阶 vue-router 路由(二)(replace属性、编程式路由导航、缓存路由组件、路由的专属钩子)
01 complexity
2022.6.12-----leetcode. eight hundred and ninety
CEPH deploy offline deployment of CEPH cluster and error reporting FAQ
When openharmony meets openeuler
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗