Python implements backup program instances under Windows

  • 2020-04-02 13:48:11
  • OfStack

Many books talk about Python backup under Linux, and on xp test can also perform backup function, the code is almost the same, but when it comes to the implementation of packaging is not the same. And to use winrar, other compressed files are the same.

First, we need to add the winrar path to the path, which is not valid until we restart the machine.
Note here: after adding winrar's path to the path, be sure to restart, or the path will not work, packaging will fail!
 
The command used here is: winrar a xxx.zip XXXX
XXX is an arbitrary character
 
Example code is as follows:


# Backup script for backup  
#Filename:backup_ver1.py 
import os 
import time 
import sys 
# Backup source file path  
sourc = ['G://test//test.txt'] 
# Where the backup files are stored  
target_dir = 'G://' 
# The name of the backup file  
target = target_dir + time.strftime('%Y%m%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 %s" %(target,' '.join(sourc)) 
print zip_command 
if os.system(zip_command) == 0: 
  print ' Packing success! '+target 
else: 
  print ' Packing failed! 

Related articles: