Python實現(xiàn)多級目錄壓縮與解壓文件的方法
本文實例講述了Python實現(xiàn)多級目錄壓縮與解壓文件的方法。分享給大家供大家參考,具體如下:
咱向來就是拿來主意,也發(fā)個東西供同行“拿來”使用吧
咱信奉的就是少量的代碼完成大量的工作,雖然代碼不多,但還是要用大腦的。發(fā)出來供大家參考
功能:
- 支持中文路徑,支持多級目錄
- 支持跨平臺,在linux和window下都可直接使用
- 壓縮的多態(tài)性
- 壓縮包不帶級父文件夾目錄壓縮
- 壓縮包帶父級文件夾目錄
- 不指定目標文件與路徑壓縮
- 指定壓縮包名稱不指定路徑壓縮
還是看代碼吧
#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;
#校驗創(chuàng)建備份路徑目錄
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);
好了!就這么多,如果你覺得有用就頂一下吧。有問題也可以聯(lián)系我
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python+Pygame實戰(zhàn)之泡泡游戲的實現(xiàn)
這篇文章主要為大家介紹了如何利用Python中的Pygame模塊實現(xiàn)泡泡游戲,文中的示例代碼講解詳細,對我們學習Python游戲開發(fā)有一定幫助,需要的可以參考一下2022-07-07
在Python中居然可以定義兩個同名通參數(shù)的函數(shù)
今天小編就為大家分享一篇在Python中居然可以定義兩個同名通參數(shù)的函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
通俗的講解深度學習中CUDA,cudatookit,cudnn和pytorch的關系
有些剛入行的朋友總是搞不清楚CUDA,cudatookit,cudnn和pytorch的關系,那么今天這篇文章用通俗易懂的話講解了他們之間的關系,需要的朋友可以參考下,相信會對你有所幫助2023-03-03
Python操作MongoDB數(shù)據(jù)庫的方法示例
這篇文章主要介紹了Python操作MongoDB數(shù)據(jù)庫的方法,結合實例形式分析了Python命令行模式下操作MongoDB數(shù)據(jù)庫實現(xiàn)連接、查找、刪除、排序等相關操作技巧,需要的朋友可以參考下2018-01-01

