解压zipfile & tarfile
2024-09-25 06:12:18
def __un_zip(self, file_path):
"""解压.zip格式文件到同名目录下,若解压之前就存在该目录说明已解压,跳过解压过程,返回该目录"""
zip_file = zipfile.ZipFile(file_path)
file_dir = file_path.split(".zip")[]
if os.path.isdir(file_dir):
return file_dir
else:
os.mkdir(file_dir)
for names in zip_file.namelist():
zip_file.extract(names, file_dir)
zip_file.close()
return file_dir def __un_tar(self, file_path):
"""解压.tar.gz格式文件到同名目录下,若解压之前就存在该目录说明已解压,跳过解压过程,返回该目录"""
tar = tarfile.open(file_path)
names = tar.getnames()
file_dir = file_path.split(".tar.gz")[]
if os.path.isdir(file_dir):
return file_dir
else:
os.mkdir(file_dir)
for name in names:
tar.extract(name, file_dir)
tar.close()
return file_dir
补充
def extractall(file_dir, to_dir):
for root, dirs, names in os.walk(file_dir):
for name in names:
file_path = os.path.join(root, name)
if file_path.endswith(".gz") or file_path.endswith(".zip") or file_path.endswith(".rar"):
unzip_file_path = os.path.join(to_dir, name)
Archive(file_path).extractall(unzip_file_path, auto_create_dir=True)
# os.remove(file_path)
file_dir = unzip_file_path
# to_dir=to_dir
# extractall(file_dir, to_dir)
最新文章
- Vs2012 创建项目失败 未找到与约束ContractName
- [hadoop] hadoop-all-in-one-伪分布式安装
- MATLAB处理信号得到频谱、相谱、功率谱
- Win7安装visual c++ 2015 redistributable x64失败
- [转]Ionic – Mobile UI Framework for PhoneGap/Cordova Developers
- 配置Java EE Eclipse+Tomcat开发环境
- 猫都能学会的Unity3D Shader入门指南
- jquery学习笔记(4)--实现table隔行变色以及单选框选中
- <;php>;json小结
- Java的HashCode
- javascript实现图片无缝滚动(scrollLeft的使用方法介绍)
- EFCore Lazy Loading + Inheritance = 干净的数据表 (一) 【献给处女座的DB First程序猿】
- 枪弹辩驳(弹丸论破)即将登陆PC
- IntelliJ IDEA(四) :Settings(上)
- CentOS6.5系统,mysql数据库的安装
- bc 命令
- yolo2详解
- zsh与oh-my-zsh
- SpringMVC实现多文件(批量)上传
- 【linux系列】Centos下安装mysql数据库
热门文章
- 启动欢迎页面时,Android Studio设置全屏Activity
- string部分方法
- 配置Beyond Compare作为比较和合并工具
- quartz的job怎么获取Spring上下文
- [Oracle][RAC]Oracle RAC环境里打OCW上的个别Patch,然后Rollback,发现OCW也被Rollback掉了
- SVG路径PATH
- Codeforces Round #309 (Div. 1) A(组合数学)
- git一些有用的命令
- 有关导出Excel特殊字符的问题
- c#的默认访问权限