没有合适的资源?快使用搜索试试~ 我知道了~
首页Python实现多级目录压缩与解压文件的方法
资源详情
资源评论
资源推荐

Python实现多级目录压缩与解压文件的方法实现多级目录压缩与解压文件的方法
主要介绍了Python实现多级目录压缩与解压文件的方法,涉及Python针对文件路径的遍历、判断以及文件压缩、
解压缩等相关操作技巧,需要的朋友可以参考下
本文实例讲述了Python实现多级目录压缩与解压文件的方法。分享给大家供大家参考,具体如下:
咱向来就是拿来主意,也发个东西供同行“拿来”使用吧
咱信奉的就是少量的代码完成大量的工作,虽然代码不多,但还是要用大脑的。发出来供大家参考
功能:
支持中文路径,支持多级目录
支持跨平台,在linux和window下都可直接使用
压缩的多态性
压缩包不带级父文件夹目录压缩
压缩包带父级文件夹目录
不指定目标文件与路径压缩
指定压缩包名称不指定路径压缩
还是看代码吧
#coding:utf-8
#压缩解压文件模块
#支持中文路径,支持多级目录
#支持跨平台,在linux和window下都可直接使用
#python 2.7.2
#author:xieShuxu
#QQ:258356793
#Email:sondx@qq.com
import zipfile,os
class ZipException(Exception):
pass
def unZipFile(zipPath,unZipPath=''):
'''解压文件
zipPath 要解压的文件路径
unZipPath 解压目标路径 默认解压到zipPath所在目录
'''
try:
filePath=filePath.decode('utf-8');
zipFilePath=zipFilePath.decode('utf-8');
except:
print '================'
if not os.path.exists(zipPath):
raise ZipException,'function unZipFile:not exists file or dir(%s)' %zipPath;
if unZipPath=='':
unZipPath=os.path.splitext(zipPath)[0];
if not unZipPath.endswith(os.sep):
unZipPath+=os.sep;
z = zipfile.ZipFile(zipPath, 'r')
#zipInfolist=z.namelist();
for k in z.infolist():
savePath=unZipPath+k.filename;
saveDir=os.path.dirname(savePath);
if not os.path.exists(saveDir):
os.makedirs(saveDir);
f=open(savePath,'wb');
f.write(z.read(k));
f.close();
z.close();
#print unZipPath
global _iterateExeZipFile;
def exeZipFile(filePath,zipFilePath=''):
'''压缩文件
filePath 要解压的文件路径 可以是文件或者目录
os.sep结尾表示压缩该目录下的子文件和文件夹 不包含该文件夹,否则包含该文件夹压缩
ZipFilePath 压缩包文件路径
也可只传文件名
默认压缩到filePath的父级目录下
'''
filePath=filePath.decode('utf-8');
zipFilePath=zipFilePath.decode('utf-8');
#压缩文件不存在直接返回
if not os.path.exists(filePath):
raise ZipException,'function exeZipFile:not exists file or dir(%s)' %filePath;
# 是否包含父级目录压缩


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0