This article describes how to implement a backup program in python in windows. if you need a backup program, you can refer to the Python backups mentioned in many books in linux, in xp, you can also perform the backup function. the code is almost the same, that is, the package is different. Winrar is also used, and the same applies to other compressed files.
First, we need to add the winrar path to the path, which is valid only after the host is restarted.
Note: after you add the winrar path to the path, you must restart it. Otherwise, the path setting will not take effect and the package will fail!
The command used here is: winrar a xxx.zip xxxx
Xxx is any character
The instance code is as follows:
# Backup script used for backup # Filename: backup_ver1.py import OS import time import sys # Source file path sourc = ['G: // test // test.txt '] # target_dir = 'G: // '# Name of the backup file target = target_dir + time.strftime('policyuncm=d?h=m=s'{}'.rar' # zip_command = "zip-qr '% s' % s" % (target ,''. join (sourc) # zip_command = "winrar a/" % s/"% s" % (target ,''. join (sourc) zip_command = "winrar a % s" % (target ,''. join (sourc) print zip_com Mand if OS. system (zip_command) = 0: print 'package successful! '+ Target else: print' packaging failed!