最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)壓縮文件夾與解壓縮zip文件的方法

 更新時(shí)間:2018年09月01日 10:21:41   作者:Hardy-Lee  
這篇文章主要介紹了Python實(shí)現(xiàn)壓縮文件夾與解壓縮zip文件的方法,涉及Python使用zipfile模塊進(jìn)行zip文件壓縮與解壓縮相關(guān)操作技巧,需要的朋友可以參考下

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

直接上代碼

#coding=utf-8
#甄碼農(nóng)python代碼
#使用zipfile做目錄壓縮,解壓縮功能
import os,os.path
import zipfile
def zip_dir(dirname,zipfilename):
  filelist = []
  if os.path.isfile(dirname):
    filelist.append(dirname)
  else :
    for root, dirs, files in os.walk(dirname):
      for name in files:
        filelist.append(os.path.join(root, name))
  zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
  for tar in filelist:
    arcname = tar[len(dirname):]
    #print arcname
    zf.write(tar,arcname)
  zf.close()
def unzip_file(zipfilename, unziptodir):
  if not os.path.exists(unziptodir): os.mkdir(unziptodir, 0777)
  zfobj = zipfile.ZipFile(zipfilename)
  for name in zfobj.namelist():
    name = name.replace('\\','/')
    if name.endswith('/'):
      os.mkdir(os.path.join(unziptodir, name))
    else:
      ext_filename = os.path.join(unziptodir, name)
      ext_dir= os.path.dirname(ext_filename)
      if not os.path.exists(ext_dir) : os.mkdir(ext_dir,0777)
      outfile = open(ext_filename, 'wb')
      outfile.write(zfobj.read(name))
      outfile.close()
if __name__ == '__main__':
  zip_dir(r'E:/python/learning',r'E:/python/learning/zip.zip')
  unzip_file(r'E:/python/learning/zip.zip',r'E:/python/learning2')

運(yùn)行后在E:/python/learning目錄下生成zip.zip壓縮文件,同時(shí)在E:/python目錄下解壓縮zip.zip文件到learning2目錄。

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

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

相關(guān)文章

最新評(píng)論

泗阳县| 开化县| 龙井市| 伊春市| 灵璧县| 襄城县| 信丰县| 滕州市| 丁青县| 四子王旗| 昭觉县| 巴中市| 宣威市| 东乡| 秭归县| 铅山县| 宁远县| 宜宾市| 新兴县| 奎屯市| 报价| 寿阳县| 沂南县| 乐都县| 无棣县| 容城县| 托克托县| 阿荣旗| 抚顺市| 神农架林区| 马山县| 景泰县| 华蓥市| 南昌县| 友谊县| 灵丘县| 静乐县| 高雄县| 玛沁县| 资源县| 宁海县|