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

首頁 > 編程 > Python > 正文

Python實現壓縮文件夾與解壓縮zip文件的方法

2020-02-15 22:52:55
字體:
來源:轉載
供稿:網友

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

直接上代碼

#coding=utf-8#甄碼農python代碼#使用zipfile做目錄壓縮,解壓縮功能import os,os.pathimport zipfiledef 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')

運行后在E:/python/learning目錄下生成zip.zip壓縮文件,同時在E:/python目錄下解壓縮zip.zip文件到learning2目錄。

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东莞市| 库车县| 岳池县| 寻乌县| 井冈山市| 阳信县| 庆安县| 军事| 来宾市| 托里县| 吴江市| 东方市| 清涧县| 郎溪县| 鹤壁市| 体育| 河北区| 临西县| 略阳县| 集贤县| 临安市| 阿尔山市| 陆河县| 嫩江县| 内江市| 巴彦县| 乡城县| 扎兰屯市| 宁乡县| 河间市| 修水县| 澄江县| 青铜峡市| 云南省| 仲巴县| 贵溪市| 阿巴嘎旗| 富蕴县| 赣州市| 连平县| 鲁甸县|