python的zipfile实现文件目录解压缩
主要是 解决了压缩目录下 空文件夹 的压缩 和 解压缩问题
压缩文件夹的函数:
# 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实现文件目录解压缩的更多相关文章
- 【转】python的zipfile压缩、解压缩
网上搜索了很多关于python的zipfile压缩.解压缩.觉得讲述比较详细,例子也很明了.由于比较懒,就直接复制了. 以下内容大部分转于 http://blog.csdn.net/jgood/art ...
- python中zipfile模块实例化解析
文章内容由--“脚本之家“--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是 ...
- Python中zipfile压缩文件模块的使用
目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...
- Python: 使用zipfile+io模块在内存中进行zip操作
#!/usr/bin/env python #coding=utf-8 ''' 版权所有 (c) 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 本代码采用 ...
- python模块--zipfile文件压缩
zipfile模块是python中一个处理压缩文件的模块,解决了不少我们平常需要处理压缩文件的需求 ,本文主要谈谈zipfile几个常用的用法. 首先我在Windows操作系统中创建如下的文件目录: ...
- python模块 zipfile
zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的zipfile里有两个非常重要的class, 分别是ZipFile和Zip ...
- python中zipfile文件名编码的问题
在python中编程导入压缩包,利用zipfile包,从zipinfo读取文件名总是出错,创建的文件名是乱码,写入pgsql更是出错. 但在ubuntu下测试却正常,在windows下测试总是失败. ...
- python用zipfile模块打包文件或是目录、解压zip文件实例
#!/usr/bin/env python # -*- coding: utf-8 -*- from zipfile import * import zipfile #解压zip文件 def unzi ...
- python 序列解包(解压缩)
序列解包(解压缩) 所学的解压缩 如果我们给出一个列表,我们需要一次性取出多个值,我们是不是可以用下面的方式实现呢? name_list = ['nick', 'egon', 'jason'] x = ...
随机推荐
- 使用System.getProperty方法,如何配置JVM系统属性 (转载)
很多时候需要在项目中读取外部属性文件,用到了System.getProperty("")方法.这个方法需要配置JVM系统属性,那么如何配置呢? 那就是使用java -D 配置系统属 ...
- uitabbar 标题设置 button text attributes only respected for UIControlStateNormal
uitabbar 标题设置 button text attributes only respected for UIControlStateNormal [[UITabBarItem appearan ...
- PHP 5 时区
PHP 5 时区 PHP 支持的时区 下面是 PHP 支持的时区的完整列表,这些对一些 PHP 日期函数很有用. 非洲 美洲 南极洲 北冰洋 亚洲 大西洋 大洋洲 欧洲 印度洋 太平洋 非洲 Afri ...
- Linux mkdir 如何递归创建目录?
mkdir 如何递归创建目录? mkdir –vp 目录1/目录2/目录3 详细介绍: linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目 ...
- js 函数节流throttle 函数去抖debounce
1.函数节流throttle 通俗解释: 假设你正在乘电梯上楼,当电梯门关闭之前发现有人也要乘电梯,礼貌起见,你会按下开门开关,然后等他进电梯: 但是,你是个没耐心的人,你最多只会等待电梯停留一分钟: ...
- 【划分树+二分】HDU 4417 Super Mario
第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...
- LoadRunner利用ODBC编写MySql脚本(转)
LoadRunner测试数据库是模拟客户端去连接数据库服务器,因此,需要协议(或者说驱动的支持).LoadRunner本身直接支持Oracle.SqlServer数据库,这两个数据库直接选择相应的协议 ...
- 【TP3.2.3】微信网页授权--基类
非常好用的微信授权 基类:其他的微信权限类都可以继承至该类: <?php namespace Wechat\Controller; use Think\Controller; //微信接口基础类 ...
- Git-学习笔记(常用命令集合)
这里引用一下百度百科里Git的内容: Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的. Git 是用 ...
- Eclipse wtp project dependent project facets问题
wtp project会编译成一个war包,但在eclipse里debug时,依赖的project没有自动编译成jar包并打包到war的lib目录里. 原因:依赖project的facets未设置或未 ...