亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python實現備份文件實例

系統 1901 0

本文實例講述了Python實現備份文件的方法,是一個非常實用的技巧。分享給大家供大家參考。具體方法如下:

該實例主要實現讀取一個任務文件, 根據指定的任務參數自動備份.

任務文件的格式: (注意,分號后面注釋是不支持的)

            
[task] ; 一項任務開始
dir=h:/Project ; 指定備份的目錄
recusive=1 ; 是否遞歸子目錄
suffix=h|cpp|hpp|c|user|filters|vcxproj|sln|css|gif|html|bmp|png|lib|dsw|dsp|htm|html|ico|ini|jpg|rc|vssscc ; 指定備份的擴展名
exclude=0 ; 指定是備份上面的參數指定的擴展名還是排除指定的擴展名
zip=Project.zip ; 備份后的文件路徑名


          

python代碼如下:

            
# -*- coding: utf-8 -*- 
import sys
import os
import zipfile
class Task:
 #dir str directory
 #bsub BOOL include subdirectory
 #sfx str postsuffix ,sepeated by '|'
 #ecld BOOL include or execlude the postsuffix sfx
 def __init__(self,dir,bsub,sfx,ecld,zip):
 self.dir = dir
 self.bsub = bsub
 self.suffix = sfx.split("|")
 self.exclude = ecld
 self.zip = zip
 
 @staticmethod
 def isfilter(sfx,sfxs,bexcld):
 bFound = False
 for e in sfxs:
  if e == sfx:
  bFound = True
  break 
 if bexcld:
  return not bFound;
 else:
  return bFound;
 
class QBackup:
 '''備份指定目錄下具備指定擴展名的文件'''
 def __init__(self):
 self._list = []
 
 def __del__(self):
 pass
 
 #tfile 任務文件
 def ReadTask(self,tfile):
 dir = ""
 bsub = False
 sfx = ""
 becld = False
 zip = ""
 try:
  f = open(tfile,'r')
  while True:
  line = f.readline()
  if len(line) == 0:
   break;
  line = line.strip(" ")
  if "[Task]/n".lower() == line.lower():
   # 讀取接下來的4行
   iline = 1
   while iline <= 5:
   line = f.readline()
   line = line.strip(" /t/n") # 去除前后的空白符 
   idx = line.find("=")
   if -1 == idx:
    break;
   atti = line[0:idx]
   value = line[idx+1:]
   print(value)
   if "dir" == atti:
    dir = value
   elif "recusive" == atti:
    bsub = bool(int(value))
   elif "suffix" == atti:
    sufix = value
   elif "exclude" == atti:
    becld = bool(int(value))
   elif "zip" == atti:
    zip = value
   else:
    break
   iline += 1
   else:
   t = Task(dir,bsub,sufix,becld,zip)
   self._list.append(t)
 except:
  return False
 return True
 
 def DoBackup(self):
 for e in self._list:
  try:
  zip = zipfile.ZipFile(e.zip,'w',zipfile.ZIP_DEFLATED)
  self.ZipDir(zip,e.dir,e.bsub,e.suffix,e.exclude)
  zip.close()
  except:
  print("exception raised!")
  return False
 return True 
 def ZipDir(self,zip,dir,bsub,sfxs,ecld):
 subdir = ""
 path = ""
 if os.path.isdir(dir):
  paths = os.listdir(dir)
  #備份本目錄
  print("ZipDir: ",dir)
  for e in paths:
  path = dir + "/" + e
  ext = os.path.splitext(e)[1][1:]
  if os.path.isfile(path) and Task.isfilter(ext,sfxs,ecld):
   print ("ZipFile: ",path)
   zip.write(path)
  #清理子目錄
  if bsub: 
  for e in paths:
   subdir = dir + "/" + e
   self.ZipDir(zip,subdir,bsub,sfxs,ecld)
 
 def PrintTask(self):
 for e in self._list:
  print (e.dir,e.bsub,e.suffix,e.exclude,e.zip)
 
if '__main__' == __name__:
 c = QBackup()
 c.ReadTask("bkup.txt")
 c.DoBackup()
 
          

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线欧美 | 国产精品18久久久久久久久久 | 91模特 | 欧美成人精品在线 | 亚洲va在线va天堂va四虎 | 亚洲免费视频一区 | 国产小视频在线观看免费 | 九九九九热 | 久久伦理影院 | 欧美老司机 | 亚洲香蕉久久一区二区三区四区 | 欧美日韩高清在线观看 | 狠狠狠狼鲁欧美综合网免费 | 日本三级日本三级人妇三级四 | 成人精品视频一区二区在线 | 91青青青国产在观免费影视 | 伊人中文字幕 | 国产在线视频h | 国产午夜永久福利视频在线观看 | 91情国产l精品国产亚洲区 | 欧美色网 | 热99re久久精品天堂vr | 高清不卡一区二区三区 | 久久中文精品 | 欧美国产综合 | 午夜91 | 久草视频观看 | 天天干天天草天天 | 狠狠干成人| 日韩精品久久久久久 | 亚洲视频手机在线观看 | 26uuu另类欧美亚洲曰本 | 国产成人精品精品欧美 | 99re这里只有精品在线观看 | 亚洲精品一二三四区 | 国产福利不卡视频在免费 | a色毛片免费视频 | 国产深夜 | 成人一级片在线观看 | 五月四房 | a在线观看免费视频 |