Python implements backup file instances

Source: Internet
Author: User
This article mainly introduces how to implement the backup file in Python, which can be used to back up files with various common extensions, for more information about how to use Python to back up files, see the example in this article. Share it with you for your reference. The specific method is as follows:

This instance is used to read a task file and automatically back up data based on specified task parameters.

Task File Format: (Note that annotations after semicolons are not supported)

[Task]; a task starts with dir = h:/Project; specifies the Backup Directory recusive = 1; recursive 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; specify the backup extension exclude = 0. It indicates that the extension name specified by the backup is also the extension name specified by the release zipw.project.zip, and the file path name after the backup.

The python code is as follows:

#-*-Coding: UTF-8-*-import sysimport osimport zipfileclass 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 = Fa Lse for e in sfxs: if e = sfx: bFound = True break if bexcld: return not bFound; else: return bFound; class QBackup: '''backup the file with the specified extension in the 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 True: line = f. readline () if len (line) = 0: break; line = line. strip ("") If "[Task]/n ". lower () = line. lower (): # Read the following four lines of iline = 1 while iline <= 5: line = f. readline () line = line. strip ("/t/n") # Remove the leading and trailing spaces. 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) handle t: 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 () failed T: 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) # back up the current directory 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) # Clear the subdirectory 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 learn Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.