Python實現(xiàn)備份文件實例
更新時間:2014年09月16日 15:59:24 投稿:shichen2014
這篇文章主要介紹了Python實現(xiàn)備份文件的方法,可實現(xiàn)針對各類常見擴展名的文件進行備份的功能,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)備份文件的方法,是一個非常實用的技巧。分享給大家供大家參考。具體方法如下:
該實例主要實現(xiàn)讀取一個任務(wù)文件, 根據(jù)指定的任務(wù)參數(shù)自動備份.
任務(wù)文件的格式: (注意,分號后面注釋是不支持的)
[task] ; 一項任務(wù)開始 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 ; 指定是備份上面的參數(shù)指定的擴展名還是排除指定的擴展名 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 任務(wù)文件
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程序設(shè)計的學習有所幫助。
您可能感興趣的文章:
相關(guān)文章
Python在信息學競賽中的運用及Python的基本用法(詳解)
下面小編就為大家?guī)硪黄狿ython在信息學競賽中的運用及Python的基本用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Win10下python3.5和python2.7環(huán)境變量配置教程
這篇文章主要為大家詳細介紹了Win10下python3.5和python2.7環(huán)境變量配置教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09

