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

Python打包文件夾的方法小結(zip,tar,tar.gz等)

 更新時間:2016年09月18日 14:10:00   作者:RQSLT  
這篇文章主要介紹了Python打包文件夾的方法,結合實例形式總結分析了Python打包成zip,tar,tar.gz等格式文件的操作技巧,需要的朋友可以參考下

本文實例講述了Python打包文件夾的方法。分享給大家供大家參考,具體如下:

一、zip

import os, zipfile
#打包目錄為zip文件(未壓縮)
def make_zip(source_dir, output_filename):
  zipf = zipfile.ZipFile(output_filename, 'w')
  pre_len = len(os.path.dirname(source_dir))
  for parent, dirnames, filenames in os.walk(source_dir):
    for filename in filenames:
      pathfile = os.path.join(parent, filename)
      arcname = pathfile[pre_len:].strip(os.path.sep)   #相對路徑
      zipf.write(pathfile, arcname)
  zipf.close()

二、tar/tar.gz

import os, tarfile
#一次性打包整個根目錄??兆幽夸洉淮虬?。
#如果只打包不壓縮,將"w:gz"參數(shù)改為"w:"或"w"即可。
def make_targz(output_filename, source_dir):
  with tarfile.open(output_filename, "w:gz") as tar:
    tar.add(source_dir, arcname=os.path.basename(source_dir))
#逐個添加文件打包,未打包空子目錄??蛇^濾文件。
#如果只打包不壓縮,將"w:gz"參數(shù)改為"w:"或"w"即可。
def make_targz_one_by_one(output_filename, source_dir):
  tar = tarfile.open(output_filename,"w:gz")
  for root,dir,files in os.walk(source_dir):
    for file in files:
      pathfile = os.path.join(root, file)
      tar.add(pathfile)
  tar.close()

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

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

相關文章

最新評論

昂仁县| 阿拉善盟| 宁波市| 新丰县| 集安市| 秦安县| 甘南县| 英德市| 洛川县| 栾川县| 钟祥市| 门头沟区| 滨海县| 沙田区| 平泉县| 大英县| 酒泉市| 墨脱县| 壤塘县| 什邡市| 南川市| 信宜市| 虞城县| 彭州市| 新乐市| 深泽县| 林州市| 聂拉木县| 莲花县| 屏南县| 遵化市| 宜丰县| 阿鲁科尔沁旗| 和林格尔县| 温泉县| 江津市| 乌苏市| 呼玛县| 瑞丽市| 乐清市| 新昌县|