一个归档的小程序。
1 # Filename: backup_ver3
2 import os
3 import time
4 #import sys
5 #reload(sys)
6 #sys.setdefaultencoding('utf8')
7 # 1. The files and directories to be backed up are specified in a list.
8 source = ['C:\\networkx.txt', 'c:\\backup\\']
9
10 # 2. The backip must be stored in a main backup directory
11 target_dir = 'C:\\backup\\'
12
13 # 3. The files are backed up into a zip file.
14
15 # 4. The current day is the name of the subdirectory in the main directory
16 today = target_dir + time.strftime('%Y%m%d')
17 # The current time is the name of the zip archive
18 now = time.strftime('%H%M%S')
19 # create the subbdirectory if it isnt already there
20 if not os.path.exists(today):
21 os.mkdir(today)
22 print 'Successfully created directory', today
23 # Take a comment from the user to create the name of the zip file
24 comment = raw_input('Enter a comment-->')
25 if len(comment) == 0:
26 target = today + os.sep + now + '.zip'
27 else:
28 target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'
29 # 5. We use the zip command
30 zip_command = "zip %s %s" %(target, ' '.join(source))
31
32 # Run the backup
33 if os.system(zip_command) == 0:
34 print 'Successful backup to', target
35 else:
36 print 'Backup FAILED'