This example describes how Python implements a backup file, and is a very useful technique. Share to everyone for your reference. The specific methods are as follows:
The main implementation of this instance is to read a task file and automatically back up the specified task parameters.
Format of the task file: (Note that the comment is not supported after the semicolon)
[Task]; A task begins
dir=h:/project; Specifies the directory recusive=1 of the backup
; whether to recursively subdirectory
suffix=h|cpp|hpp|c|user|filters|vcxproj|sln|css| GIF|HTML|BMP|PNG|LIB|DSW|DSP|HTM|HTML|ICO|INI|JPG|RC|VSSSCC; Specifies the extension of the backup
exclude=0; Specifies whether the extension specified by the parameter above is backed up or excludes the specified extension
zip=project.zip; The file path name after the backup
The Python code is as follows:
#-*-coding:utf-8-*-import OS import zipfile class Task: #dir str directory #bsub BOOL include Subdirec
Tory #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: "Backup files with specified extensions under specified directory ' def __init__ (self): Self._list = [] def __del__ (self): Pass #tfile Task file def readtask (self,tfile): dir = "" BSub = False SFX = "" BECLD = False zip = "" Try:f = Open (Tfile, ' R ') while T
Rue:line = F.readline () If Len (line) = = 0:break; line = Line.strip ("") if "[task]/n". lower () = = Line.lower (): # Read the next 4 lines iline = 1 while iline <= 5:line = F.readline () line = Line.strIP ("/t/n") # Remove the whitespace before and after 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:zi p = 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): Pat HS = 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 ()
I hope this article will help you with your study of Python programming.