日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法

發(fā)布時(shí)間:2024/9/19 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文實(shí)例講述了Python實(shí)現(xiàn)多級(jí)目錄壓縮與解壓文件的方法。分享給大家供大家參考,具體如下:

咱向來(lái)就是拿來(lái)主意,也發(fā)個(gè)東西供同行“拿來(lái)”使用吧

咱信奉的就是少量的代碼完成大量的工作,雖然代碼不多,但還是要用大腦的。發(fā)出來(lái)供大家參考

功能:

支持中文路徑,支持多級(jí)目錄

支持跨平臺(tái),在linux和window下都可直接使用

壓縮的多態(tài)性

壓縮包不帶級(jí)父文件夾目錄壓縮

壓縮包帶父級(jí)文件夾目錄

不指定目標(biāo)文件與路徑壓縮

指定壓縮包名稱不指定路徑壓縮

還是看代碼吧

#coding:utf-8

#壓縮解壓文件模塊

#支持中文路徑,支持多級(jí)目錄

#支持跨平臺(tái),在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 解壓目標(biāo)路徑 默認(rèn)解壓到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結(jié)尾表示壓縮該目錄下的子文件和文件夾 不包含該文件夾,否則包含該文件夾壓縮

ZipFilePath 壓縮包文件路徑

也可只傳文件名

默認(rèn)壓縮到filePath的父級(jí)目錄下

'''

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;

# 是否包含父級(jí)目錄壓縮

hasPDir=not filePath.endswith(os.sep);

if not hasPDir:

filePath=os.path.dirname(filePath);

print filePath

#校驗(yàn)備份文件路徑

if zipFilePath=='':

zipFilePath=os.path.splitext(filePath)[0]+'.zip';

elif zipFilePath.find(os.sep)==-1:#只傳文件名的處理

zipFilePath=os.path.dirname(filePath)+os.sep+zipFilePath;

#校驗(yàn)創(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];

#開始?jí)嚎s

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__':

#壓縮包不帶級(jí)父文件夾目錄

testdir='D:\\codeSource\\linuxAgent\\'

zipFilePath='D:\\codeSource\\壓縮包不帶父級(jí)目錄.zip'

exeZipFile(testdir,zipFilePath);

#壓縮包帶父級(jí)文件夾目錄

testdir='D:\\codeSource\\linuxAgent'#不帶后綴斜線

zipFilePath='D:\\codeSource\\壓縮包帶父級(jí)目錄.zip'

exeZipFile(testdir,zipFilePath);

#不指定目標(biāo)文件與路徑壓縮

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);

好了!就這么多,如果你覺(jué)得有用就頂一下吧。有問(wèn)題也可以聯(lián)系我

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

本文標(biāo)題: Python實(shí)現(xiàn)多級(jí)目錄壓縮與解壓文件的方法

本文地址: http://www.cppcns.com/jiaoben/python/238776.html

總結(jié)

以上是生活随笔為你收集整理的python遍历目录压缩文件夹_Python实现多级目录压缩与解压文件的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。