python解压TAR文件至指定文件夹的实例

yipeiwu_com6年前Python基础

如下所示:

######### Extract all files from src_dir to des_dir
def extract_tar_files(src_dir,des_dir):
  files = os.listdir(src_dir)
  for file in files:
    dir_tmp = os.path.join(src_dir, file)
    print dir_tmp
    if not os.path.isdir(dir_tmp): ##是文件,非文件夹
      #解压特定文件
      if dir_tmp.endswith("gz") and (dir_tmp.find(cs.Port_week_perfer_name_start) != -1):
        #f = zipfile.ZipFile(dir_tmp, mode="r")
        f = tarfile.open(dir_tmp)
        names = f.getnames()
        for name in names:
          f.extract(name, path=des_dir)
    else:
      extract_tar_files(dir_tmp,des_dir)
  return 0

以上这篇python解压TAR文件至指定文件夹的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中线程编程之threading模块的使用详解

threading.Thread Thread 是threading模块中最重要的类之一,可以使用它来创建线程。有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法;另...

django启动uwsgi报错的解决方法

django启动uwsgi报错的解决方法

uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。 要注意 W...

对numpy中数组元素的统一赋值实例

Numpy中的数组整体处理赋值操作一直让我有点迷糊,很多时候理解的不深入。今天单独列写相关的知识点,进行总结一下。 先看两个代码片小例子: 例子1: In [2]: arr =np....

详解mac python+selenium+Chrome 简单案例

详解mac python+selenium+Chrome 简单案例

第一步:下载selenium pip install selenium 第二步:下载和你当前谷歌浏览器对应的驱动Chromedriver 下载地址:https://npm.ta...

Python3中的2to3转换工具使用示例

python3与python2的还是有诸多的不同,比如说在2中: 复制代码 代码如下: print "Hello,World!"  raw_input()  在...