初学Python,整理了下书上的例程,做为学习的第一个实用程序。
-
-
-
-
- import os
- import time
-
-
- source = [r'E:\360Downloads']
-
-
- target_dir = 'E:\\backup\\'
-
-
- today = target_dir + time.strftime('%Y%m%d')
- now = time.strftime('%H%M%S')
-
-
- comment = input('Enter a comment:')
- if len(comment) == 0:
- target = today + os.sep + now + '.zip'
- else:
- target = today + os.sep + now + '_' + \
- comment.replace(' ', '_') + '.zip'
-
-
- if not os.path.exists(today):
- os.mkdir(today)
- print('Successfully created directory', today)
-
-
- zip_command = "winrar a %s %s" %(target, ' '.join(source))
-
-
- if os.system(zip_command) == 0:
- print('Successful backup to', target)
- else:
- print('Backup failed')