1. 介绍
通过实例学习Python的使用,该实例来自文献[1]中的第11章解决问题。
由于没有搞清楚Win7下如何通过命令行调用zip命令,所以采用7z[2],采用7-zip命令行版本[3],版本号为7-zip 9.20,下载后需要配置环境变量,修改Path,使其包含7za.exe所在目录。
软硬件配置情况:
Win7旗舰版 SP1
Python 3.4
7-zip 9.20 命令行版本(需要配置环境变量Path)
2. 问题
写一个能给我所有重要文件建立备份的程序。
程序如何工作:
1.需要备份的文件和目录由一个列表指定。
2.备份应该保存在主备份目录中。
3.文件备份成一个zip文件。
4.zip存档的名称是当前的日期和时间。
5.使用标准的zip命令。它通常默认的随Linux/ Unix发行版提供。Windows用户可以从GnuWin32项目安装。注意你可以使用任何存档命令,只要它有命令行界面就可以了,那样的话我们可以从我们的脚本中传递参数给它。(本文使用了7-zip 9.20命令行版本)
解决方案来自参考文献[1]。
2.1 解决方案1
实现功能:
压缩文件备份到主目录下,文件名已目前的日期时间为名称。
代码如下所示:
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 = [‘"D:\\QT\\py 1"‘,‘D:\\QT\\py2‘] 9 # Notice we had to use double quotes inside the string for names with spaces in it 10 11 # 2. The backup must be stored in a main backup directory 12 target_dir = ‘D:\\PYBackup‘ 13 14 # 3. The files are backed up into a zip file 15 # 4. The name of the zip archive is the current date and time 16 target = target_dir + os.sep + time.strftime(‘%Y%m%d%H%M%S‘) + ‘.zip‘ 17 18 # 5. We use the 7za command to put the files in a zip archive 19 zip_command = ‘7za.exe a {0} {1}‘.format(target, ‘ ‘.join(source)) 20 21 # Run the backup 22 if os.system(zip_command) == 0: 23 print (‘Successful backup to ‘, target) 24 else: 25 print (‘Backup Failed‘)
运行结果:
2.2 解决方案2
实现功能:
更好的文件名机制:使用时间作为文件名,而当前的日期作为目录名,存在在主备份目录中。这样做的优势:
(1)你的备份会以等级结构存储,因此它就更容易管理了。
(2)文件名的长度变短。
(3)采用各自独立的文件夹可以帮助你方便的检验你是否在每一天创建了备份。
代码如下所示:
1 #! /usr/bin/python 2 # Filename: backup_ver2.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 = [‘"D:\\QT\\py 1"‘,‘D:\\QT\\py2‘] 9 # Notice we had to use double quotes inside the string for names with spaces in it 10 11 # 2. The backup must be stored in a main backup directory 12 target_dir = ‘D:\\PYBackup‘ # Remember to change this to what you will be using 13 14 # 3. The files are backed up into a zip file. 15 # 4. The current day is the name of the subdirectory in the main directory 16 today = target_dir + os.sep + time.strftime(‘%Y%m%d‘) 17 # The current time is the name of the zip archive 18 now = time.strftime(‘%H%M%S‘) 19 20 # Create the subdirectory if it isn‘t already there 21 if not os.path.exists(today): 22 os.mkdir(today) # make directory 23 print (‘Successfully created directory‘,today) 24 25 # The name of the zip file 26 target = today + os.sep + now + ‘.zip‘ 27 28 # 5. We use the 7za command to put the files in a zip archive 29 zip_command = ‘7za.exe a {0} {1}‘.format(target, ‘ ‘.join(source)) 30 31 # Run the backup 32 if os.system(zip_command) == 0: 33 print (‘Successful backup to ‘, target) 34 else: 35 print (‘Backup Failed‘)
运行结果:
2.3 解决方案3
实现功能:
通过在zip归档名上附带一个用户提供的注释来提高备份的可理解性。
代码如下所示:
1 #! /usr/bin/python 2 # Filename: backup_ver3.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 = [‘"D:\\QT\\py 1"‘,‘D:\\QT\\py2‘] 9 # Notice we had to use double quotes inside the string for names with spaces in it 10 11 # 2. The backup must be stored in a main backup directory 12 target_dir = ‘D:\\PYBackup‘ # Remember to change this to what you will be using 13 14 # 3. The files are backed up into a zip file. 15 # 4. The current day is the name of the subdirectory in the main directory 16 today = target_dir + os.sep + time.strftime(‘%Y%m%d‘) 17 # The current time is the name of the zip archive 18 now = time.strftime(‘%H%M%S‘) 19 20 # Take a comment from the user to create the name of the zip file 21 comment = input (‘Enter a comment --> ‘) 22 if len(comment) == 0: # check if a comment was entered 23 target = today + os.sep + now + ‘.zip‘ 24 else: 25 target = today + os.sep + now + ‘_‘ +26 comment.replace(‘ ‘, ‘_‘) + ‘.zip‘ 27 28 # Create the subdirectory if it isn‘t already there 29 if not os.path.exists(today): 30 os.mkdir(today) # make directory 31 print (‘Successfully created directory‘,today) 32 33 # 5. We use the 7za command to put the files in a zip archive 34 zip_command = ‘7za.exe a {0} {1}‘.format(target, ‘ ‘.join(source)) 35 36 # Run the backup 37 if os.system(zip_command) == 0: 38 print (‘Successful backup to ‘, target) 39 else: 40 print (‘Backup Failed‘)
运行结果:
输入Backup
没有输入任何值,直接按回车
3. 总结
看到自己编写的程序能够运行并完成预期的功能,感觉挺爽!
Python,还没入门,还有好多的路要走,加油!
参考文献
[1] Swaroop, C.H.(著), Let it be!(译)(e-mail: 329974248@qq.com), A Byte of Python v1.92(for Python 3.0), 2011.7.9
[2] 7z, http://sparanoid.com/lab/7z/
[3] 7-zip 命令行版本(7-zip 9.20), http://downloads.sourceforge.net/sevenzip/7za920.zip