Windows下实现《python简明教程》中的压缩备份脚本

《python简明教程》的第十章中编写Python脚本来压缩备份的程序在windows下无法运行。在网上翻了半天,自己也捣鼓了半天终于弄好了。出错原因是因为windows的DOS里没有zip命令。

实现完整代码:

 1 #!/usr/bin/python

 2 # Filename: backup_ver1.py

 3 

 4 import os

 5 import time

 6 

 7 # 1.The files and directories to be backed up are specified in a list

 8 source = [r'E:\rar',r'D:\zip']

 9 

10 # 2.The backup must be stored in a main backup directory

11 target_dir = r'F:\backup\\'

12 

13 # 3.The files are backed up into a zip file.

14 # 4.The name of the zip archive is the current date and time

15 target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'

16 

17 # 5.we use the zip command (in Unix/Linux) to put the files in a zip archive

18 zip_command = r"""C:\Progra~1\WinRAR\Rar.exe a %s %s""" % (target, ' '.join(source))

19 

20 # Run the backup

21 if os.system(zip_command) ==0:

22     print 'Successful backup to ',target

23 else:

24     print 'Backup FAILED'

运行后的结果:

 

你可能感兴趣的:(windows)