The example in this paper describes how Python implements backup files, which is a very practical technique. Share to everyone for your reference. Here's how:
This instance mainly implements reading a task file, which is automatically backed up according to the specified task parameters.
The format of the task file: (note that the comment after the semicolon is not supported)
[Task]; A task begins to Dir=h:/project; Specifies the directory recusive=1 for 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 to back up the extension specified by the above parameter or to exclude the specified extension zip=project.zip; File path name after backup
The Python code is as follows:
#-*-coding:utf-8-*-import sysimport osimport zipfileclass Task: #dir str directory #bsub BOOL include subdirectory #s FX STR postsuffix, sepeated by ' | ' #ecld BOOL include or execlude the Postsuffix SFX def __init__ (self,dir,bsub,sfx,ecld,z IP): self.dir = dir self.bsub = bsub Self.suffix = sfx.split ("|") Self.exclude = ecld Self.zip = Zip @staticmethod def is Filter (SFX,SFXS,BEXCLD): Bfound = False for e in sfxs:if e = = Sfx:bfound = True if Bexcld:return not bfound; Else:return Bfound; Class Qbackup: "Backup file with specified extension in specified directory" "Def __init__ (self): Self._list = [] def __del__ (self): Pass #tfile task file def Readt Ask (Self,tfile): dir = "" BSub = False SFX = "" BECLD = False zip = "" Try:f = Open (Tfile, ' R ') while True:line = F.re Adline () If Len (line) = = 0:break; line = Line.strip ("") if "[task]/n". lower () = = Line.lower (): # reads the next 4 lines iline = 1 while iline <= 5:line = f. ReadLine () line = Line.strip ("/t/n") # before and after removing the whitespace character 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 = O S.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 is helpful to the study of Python program design.