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. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

  2. 『TensorFlow』张量拼接_调整维度_切片

    1.tf.concat tf.concat的作用主要是将向量按指定维连起来,其余维度不变:而1.0版本以后,函数的用法变成: t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, ...

  3. mybat使用注解的方式如@Select写sql

    package com.polymer.app.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibati ...

  4. Backpack VI

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  5. LAMP架构上(一)

    第十七课LAMP架构上(一) 目录 一.LAMP架构介绍 二.MySQL.MariaDB介绍 三.MySQL安装 四.MariaDB安装 五.Apache安装 六.安装PHP5 七.安装PHP7 八. ...

  6. C基础学习笔记

    1.C语言运算符优先级: 2.三种循环比较 while.do-while和for三种循环在具体的使用场合上是有区别的,如下: 1).在知道循环次数的情况下更适合使用for循环: 2).在不知道循环次数 ...

  7. ES6 promise学习笔记 -- 基本用法

    ES6 规定,Promise对象是一个构造函数,用来生成Promise实例. 下面代码创造了一个Promise实例. const promise = new Promise(function(reso ...

  8. 内存泄露java.lang.OutOfMemoryError: PermGen space解决方法

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...

  9. httpd does not appear to be running and proxying cobbler, or SELinux is in the way.

    当我们执行cobbler check时,出现这种错误:httpd does not appear to be running and proxying cobbler, or SELinux is i ...

  10. 一条命令将windows下多个ts文件合并为一个ts文件

    首先在待合并的文件夹下创建concat.bat(名字随意啦),写入如下命令 copy /b "%~dp0"\*.ts "%~dp0"\new.ts 执行该命令后 ...