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;
# 是否包含父级目录压缩
hasPDir=not filePath.endswith(os.sep);
if not hasPDir:
filePath=os.path.dirname(filePath);
print filePath
#校验备份文件路径
if zipFilePath=='':
zipFilePath=os.path.splitext(filePath)[0]+'.zip';
elif zipFilePath.find(os.sep)==-1:#只传文件名的处理
zipFilePath=os.path.dirname(filePath)+os.sep+zipFilePath;
#校验创建备份路径目录
if not os.path.exists(os.path.dirname(zipFilePath)):
os.makedirs(os.path.dirname(zipFilePath));
#初始化压缩包中的根目录
zipRoot='';
if hasPDir:
zipRoot=os.path.split(filePath)[1];
#开始压缩
z = zipfile.ZipFile(zipFilePath, 'w')
if os.path.isfile(filePath):
z.write(filePath,os.path.split(filePath)[1]);
else:
_iterateExeZipFile(filePath,zipRoot,z);
z.close();
def _iterateExeZipFile(dirPath,zipRoot,z):
压缩使用的例子:
if __name__=='__main__':
#压缩包不带级父文件夹目录
testdir='D:\\codeSource\\linuxAgent\\'
zipFilePath='D:\\codeSource\\压缩包不带父级目录.zip'
exeZipFile(testdir,zipFilePath);
#压缩包带父级文件夹目录
testdir='D:\\codeSource\\linuxAgent'#不带后缀斜线
zipFilePath='D:\\codeSource\\压缩包带父级目录.zip'
exeZipFile(testdir,zipFilePath);
#不指定目标文件与路径压缩
testdir='D:\\codeSource\\linuxAgent'
exeZipFile(testdir);
#指定压缩包名称不指定路径压缩
testdir='D:\\codeSource\\linuxAgent\\'
exeZipFile(testdir,'仅指定名称压缩包.zip');
解压的例子:
#指定解压目录解压文件
testdir=u'D:\\codeSource\\仅指定名称压缩包\\'
zipFilePath=u'D:\\codeSource\\仅指定名称压缩包.zip'
unZipFile(zipFilePath,testdir);
#不指定目录解压
zipFilePath=u'D:\\codeSource\\仅指定名称压缩包.zip'
unZipFile(zipFilePath);
好了!就这么多,如果你觉得有用就顶一下吧。有问题也可以联系我
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
本文标题: Python实现多级目录压缩与解压文件的方法
本文地址: http://www.cppcns.com/jiaoben/python/238776.html
python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法相关推荐
- Excel记录指定文件夹下的所有文件名;批量解压压缩包,处理压缩包套压缩包问题;
10.20 发现一个问题: 当压缩包过大,文件条目超过1048576时,rarfile库中的代码已经不能解决这个压缩包了,需要加装unrar库(from unrar import rarfile)和u ...
- python生成多级文件夹_Python zipfile压缩文件和文件夹(支持多级目录)
支持压缩单个文件和多级文件夹 使用方法 zip_compress(to_zip,save_zip_name):#to_zip表示源文件或者源目录,save_zip_name表示目的zip文件(可带目录 ...
- python查找指定文件夹_python实现在目录中查找指定文件的方法
本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 复制代码 代码如下: import os from glob import glob ...
- python备份目录下文件夹_python实现备份目录的方法
本文实例讲述了python实现备份目录的方法.分享给大家供大家参考.具体如下: 备份脚本1: ? 输出: $ python backup_ver1.py Successful backup to /m ...
- linux解压rar多层文件夹,linux rar 解压文件夹_rar文件夹
原标题:linux rar 解压文件夹_rar文件夹 本经验主要针对linux中的解压缩进行讲解工具/原料linux电脑一台linux文件解压缩1目前 rar a jpg.rar *.jpg //ra ...
- 在linux解压文件夹,在linux 下解压 rar 文件
linux 中,要解压 rar 文件需要 安装 rarlinux 工具,我用的是 Centos 7.1 x64 首先下载 tar 包,我下载的是 rarlinux-x64-3.8.0.tar.gz 我 ...
- WinRAR压缩解压文件
使用WinRAR压缩管理器压缩解压文件详细步骤如下: ■ 压缩文件 ① 鼠标右键需要压缩的文件,点击"添加到压缩文件",具体操作步骤如图所示: ② 压缩后的对应文件压缩包会显示在桌 ...
- 宝塔解压文件,通过SSH命令解压缩.tar.gz、.gz、.zip文件的方法
一般在linux下,常用的压缩格式有如下几个: .tar.gz..gz..zip 解压 .tar.gz 文件命令: tar -zxvf xxx.tar.gz 解压 .gz 文件命令: gunzip x ...
- 深度解读基于commons-compress解压文件——7z与常规解压
简介 java解压文件的方式有很多种,Apache官方提供了一个工具,可以用来解压很多类型的文件.该工具可以解压和压缩带密码的7z文件,并支持ar, arj, cpio, dump, tar, zip ...
最新文章
- 将Model对象转换成json文本或者json二进制文件
- 正则表达式基础知识及应用(用于个人学习以及回顾)
- POJ3696-The Luckiest number【数论,欧拉定理】
- 用计算机怎么打出狂浪字谱,狂浪歌曲简谱
- warning no newline at the end of file
- 中缀表达式转后缀表达式两位数_再见,正则表达式!
- timer数据库总显示连接不上服务器,asp.net 上传服务器后,MSSQL数据库连接问题
- android调用web接口,Android调用WebService系列之请求调用
- 剑指:合并两个排序的链表
- 双android手机同步工具,手机同步软件Android Manager使用图文教程
- 佳能Canon PIXMA MP630 一体机驱动
- css font属性详解
- Linux重定向console口控制台,Linux重定向console口控制台(Fedora)
- 02.配置免费图床Gitee/Github
- slave-pending-jobs-size-max导致主从延迟
- ICESat2学习笔记5 :ICESat-2数据下载
- 使用runOnUiThread更新UI
- word文档编辑受限怎么解除
- html入门之用html给女朋友写封精致的情书--小白直接拿去用,一点难度都没有
- 日本那些最受欢迎的产品是如何设计的?
热门文章
- MyBatis入门实例-包括实体类与数据库字段对应CLOB字段处理
- Redis 一个key-value存储系统 简介
- Kafka、RabbitMQ、RocketMQ等消息中间件的对比 —— 消息发送性能和区别
- GitLab 安装方法
- TP返回原生SQL语句:fetchSql
- PHP框架的ORM思想:O类的实例化 R数据表 M映射XML
- linux 共享内存陷井,linux共享内存应用与陷阱
- auto errored after 报错解决_css重点知识和bug解决方法
- PHP中的const
- module是什么类型_Linux驱动开发:为什么教程都不讲MODULE_DEVICE_TABLE的作用