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)

  

解压zipfile & tarfile的更多相关文章

  1. Python3学习之路~5.6 shutil & zipfile & tarfile模块

    高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])#将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(s ...

  2. Python压缩及解压文件

    Zip压缩 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import zipfile #加载模块 # 压缩 z = zipf ...

  3. 模块 - random/string/os/sys/shutil/zipfile/tarfile

    random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...

  4. time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结

    time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...

  5. python-压缩解压

    压缩解压包 #导入模块 import zipfile #新建压缩包并将db与ooo.xml压缩到文件中 z = zipfile.ZipFile('laxi.zip','w') z.write('db' ...

  6. 模块 shutil_zipfile_tarfile压缩解压

    shutil_zipfile_tarfile压缩解压 shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) #将 ...

  7. zipfile tarfile模块

    zipfile --- 使用ZIP存档 这个模块提供了创建.读取.写入.添加及列出 ZIP 文件的工具 # 创建一个ZipFile对象, 可使用上下文管理 with class zipfile.Zip ...

  8. Java实现Zip压缩包解压

        对zip压缩包的解压是比较常见的应用场景,java代码的实现也很简单.废话不多说,直接上代码吧 一.代码 /** * zip解压 * @param srcFile zip源文件 * @para ...

  9. Java 解压 zip 文件

    代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...

随机推荐

  1. Type I and type II errors | 第一类错误和第二类错误

    偶尔能看懂,但是死活记不住,归根结底是没有彻底理解! Type I and type II errors - wiki type I error is the rejection of a true  ...

  2. spring cloud 下载依赖慢解决方案

    可以在修改pom文件添加如下代码: <repositories> <repository> <id>spring-snapshots</id> < ...

  3. learning makeflie wildward character

    “?”             匹配一个任意字符 “*”                匹配0个或任意多个字符,也就是可以匹配任何内容 “[]”       匹配中括号中任意一个字符.例如:[abc] ...

  4. jdk安装环境变量配置

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  5. python之路-----多线程与多进程

    一.进程和线程的概念 1.进程(最小的资源单位): 进程:就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 程序:我们编写的程序用来描述进程要完成哪些功能以 ...

  6. qssp2017 source

    ftp://ftp.gfz-potsdam.de/pub/home/turk/wang

  7. CodeForces - 589B(暴力+排序)

    Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n ...

  8. mysql8.0.13 的docker镜像安装

    1.从docker中获取mysql8.0.13镜像 docker pull mysql:8.0.13通过 docker images 命令查看镜像是否获取到了 2.运行 mysql8.0.13 镜像 ...

  9. ulimit系统资源的设定

    使用ulimit -a 可以查看系统使用的资源 core file size 设定core文件的最大值,单位为区块,如果指定为0,不会产生core文件 data seg size 设定数据段的最大值, ...

  10. 获取map集合中key、value

    获取Map集合类中key.value的两种方法 方法一:利用Set集合中的keySet()方法 Map<String,String> map = new HashMap<String ...