国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實(shí)現(xiàn)多級(jí)目錄壓縮與解壓文件的方法

2020-01-04 14:35:58
字體:
供稿:網(wǎng)友

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

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

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

功能:

  • 支持中文路徑,支持多級(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.comimport zipfile,osclass ZipException(Exception):    passdef 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 unZipPathglobal _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);

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

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江阴市| 明水县| 定陶县| 葫芦岛市| 阳高县| 沙坪坝区| 会昌县| 洛隆县| 大足县| 木兰县| 昔阳县| 兴隆县| 永昌县| 临沧市| 洞头县| 博白县| 洞头县| 赤壁市| 大渡口区| 昌图县| 江陵县| 天峨县| 洪湖市| 枣庄市| 凭祥市| 南丰县| 万盛区| 南川市| 大丰市| 泰宁县| 大连市| 青川县| 开鲁县| 阳山县| 长乐市| 米脂县| 镶黄旗| 霍林郭勒市| 纳雍县| 阳曲县| 同仁县|