主要是 解决了压缩目录下 空文件夹 的压缩 和 解压缩问题

压缩文件夹的函数:

 # coding:utf-
import os
import zipfile def zipdir(dirToZip,savePath):
if not os.path.isdir(dirToZip):
raise Exception,u"zipDir Error,not a dir'%'".format(dirToZip) (saveDir,_)=os.path.split(savePath)
if not os.path.isdir(saveDir):
os.makedirs(saveDir) zipList = [] for root,dirs,files in os.walk(dirToZip):
for fileItem in files:
zipList.append(os.path.join(root,fileItem))
for dirItem in dirs:
zipList.append(os.path.join(root,dirItem)) zf = zipfile.ZipFile(savePath,'w',zipfile.ZIP_DEFLATED) for tar in zipList:
if tar != dirToZip:
zf.write(tar,tar.replace(dirToZip,''))
else:
zf.write(tar) zf.close()

解压的函数:

 def unZipFile(unZipSrc,targeDir):
if not os.path.isfile(unZipSrc):
raise Exception,u'unZipSrc not exists:{0}'.format(unZipSrc) if not os.path.isdir(targeDir):
os.makedirs(targeDir) print(u'开始解压缩文件:{0}'.format(unZipSrc)) unZf = zipfile.ZipFile(unZipSrc,'r') for name in unZf.namelist() :
unZfTarge = os.path.join(targeDir,name) if unZfTarge.endswith("/"):
#empty dir
splitDir = unZfTarge[:-]
if not os.path.exists(splitDir):
os.makedirs(splitDir)
else:
splitDir,_ = os.path.split(targeDir) if not os.path.exists(splitDir):
os.makedirs(splitDir) hFile = open(unZfTarge,'wb')
hFile.write(unZf.read(name))
hFile.close() print(u'解压缩完毕,目标文件目录:{0}'.format(targeDir)) unZf.close()

调用:

if __name__ == '__main__':
dirpath = os.path.abspath(u'.\\new')
savepath = os.path.abspath(u'.\\new.zip') # zipdir(dirpath,savepath)
unZipFile(savepath,dirpath)

python的zipfile实现文件目录解压缩的更多相关文章

  1. 【转】python的zipfile压缩、解压缩

    网上搜索了很多关于python的zipfile压缩.解压缩.觉得讲述比较详细,例子也很明了.由于比较懒,就直接复制了. 以下内容大部分转于 http://blog.csdn.net/jgood/art ...

  2. python中zipfile模块实例化解析

    文章内容由--“脚本之家“--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是 ...

  3. Python中zipfile压缩文件模块的使用

    目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...

  4. Python: 使用zipfile+io模块在内存中进行zip操作

    #!/usr/bin/env python #coding=utf-8 ''' 版权所有 (c) 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 本代码采用 ...

  5. python模块--zipfile文件压缩

    zipfile模块是python中一个处理压缩文件的模块,解决了不少我们平常需要处理压缩文件的需求 ,本文主要谈谈zipfile几个常用的用法. 首先我在Windows操作系统中创建如下的文件目录: ...

  6. python模块 zipfile

    zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的zipfile里有两个非常重要的class, 分别是ZipFile和Zip ...

  7. python中zipfile文件名编码的问题

    在python中编程导入压缩包,利用zipfile包,从zipinfo读取文件名总是出错,创建的文件名是乱码,写入pgsql更是出错. 但在ubuntu下测试却正常,在windows下测试总是失败. ...

  8. python用zipfile模块打包文件或是目录、解压zip文件实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...

  9. python 序列解包(解压缩)

    序列解包(解压缩) 所学的解压缩 如果我们给出一个列表,我们需要一次性取出多个值,我们是不是可以用下面的方式实现呢? name_list = ['nick', 'egon', 'jason'] x = ...

随机推荐

  1. (剑指Offer)面试题46:求1+2+3+....+n

    题目: 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字及条件判断语句(a?b:c). 思路: 1.构造函数 在类中定义静态成员变量N和 ...

  2. ASPX导入JS,JavaScript乱码怎么办

    不管你把JS改成UTF-8还是ASCII格式,弹出都是乱码. 你只要在ASPX文件顶部加上"ResponseEncoding="gb2312" ContentType=& ...

  3. I/O复用的应用场合

    I/O复用(select.poll)典型使用在下列网络应用场合: (1)当客户处理多个描述字(通常是交互式输入和网络套接口)时,必须使用I/O复用. (2)一个客户同时处理多个套接口是可能的,不过比较 ...

  4. xftp Initialize Flexnet Service failed / Error code: 50003

      xftp Initialize Flexnet Service failed / Error code: 50003 CreateTime--2018年5月3日15:47:05 Author:Ma ...

  5. SecureCRT 常用配置

    1.SecureFx 中文乱码,应设置成utf-8编码了,依旧乱码 在 C:\Users\root\AppData\Roaming\VanDyke\Config\Sessions 下找到对应的sess ...

  6. OFBiz:扩展controller.xml

    如何扩展controller.xml?两种方法:一种方法是直接修改原先的controller.xml,这种方法不方便后续升级.第二种方法是新建一个extended.xml文件,再在controller ...

  7. JAVA中的抽象类与接口

    抽象类 abstract class 包含抽象方法的类,叫抽象类.而抽象的概念就是抽象出共同属性:成员变量和方法.所以抽象类可以有private等多种权限的成员变量和非abstract的成员方法.当然 ...

  8. winform通过网络获取用户信息

    1.获取当前部署: public static NameValueCollection GetQueryStringParameters() { NameValueCollection nameVal ...

  9. 初始化列表(const和引用成员)、拷贝构造函数

    一.构造函数初始化列表 推荐在构造函数初始化列表中进行初始化 构造函数的执行分为两个阶段 初始化段 普通计算段 (一).对象成员及其初始化  C++ Code  1 2 3 4 5 6 7 8 9 1 ...

  10. centos7删除原docker 安装新docker-ce

    这里用阿里云的镜像源,速度会快很多: yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager -- ...