Python 学习笔记: 备份工具

 1 #!/usr/bin/python
 2 #filename:backup_ver3.py
 3 import os
 4 import time
 5 
 6 source=['/media/Work/WorkSpaces/gooapp','/media/Work/WorkSpaces/RsaTool']
 7 target_dir='/media/Work/backup/'
 8 today = target_dir + time.strftime('%Y%m%d')
 9 now = time.strftime('%H%M%S')
10 
11 comment = raw_input('Enter a comment:')
12 if len(comment) == 0:
13     target = today + os.sep + now + '.tar'
14 else:
15     target = today + os.sep + now + '_' + comment.replace(' ','_'+ '.tar'
16             
17 if not os.path.exists(today):
18     os.mkdir(today)
19     print 'Sucessfully created directory',today
20     
21 tar_command = 'tar -cvzf %s %s' % (target, ' '.join(source)) 
22 
23 if os.system(tar_command) == 0:
24     print 'Successful backup to',target
25 else:
26     print 'Backup FAILED'
27 
原文地址:https://www.cnblogs.com/mobile/p/1371978.html