- print '======================================================'
- print '# 功能: 备份桌面文件(带注释) '
- print '# 说明: 请首先安装7z软件,配置7z的环境变量(Path) '
- print '# 具体情况可以问Jeff Yu '
- print '======================================================'
- import os
- import time
- user = raw_input('请输入登陆电脑的用户名 比如: \\'Jeff_Yu\\' --> ')
- # 1. 要备份的文件或者目录
- source = [r'C:\\Users\\\\'+ user +'\\Desktop']
- # 2. 备份的主路径
- #target_dir = r'D:\\每日备份\\桌面备份'
- path = raw_input('请输入备份的主路径,默认路径为:"D:\\每日备份\\桌面备份",使用默认路径请按回车键 --> ')
- if path == '':
- target_dir = 'D:\\每日备份\\桌面备份'
- else:
- target_dir = path
- # 3. 备份的详细路径
- today = target_dir + '\\\\' + time.strftime('%Y') + '_'+ time.strftime('%m') + '_' + time.strftime('%d')
- now = time.strftime('%H') + '_' + time.strftime('%M') + '_' + time.strftime('%S')
- # 如果文件不存在,创建文件
- if not os.path.exists(today):
- os.makedirs(today)
- print 'Successfully created directory', today
- # 备份文件的名称
- comment = raw_input('请输入备份的注释 (不想输入,直接按回车键跳过) --> ')
- if len(comment) == 0:
- target = today + os.sep + now + '.zip'
- else:
- target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'
- # 7z 命令
- zip_command = "7z a %s %s" % (target, ' '.join(source))
- # 执行命令
- if os.system(zip_command) == 0:
- print '成功备份到:', target
- else:
- print 'Backup FAILED'
- raw_input('请按回车键退出 --> ')
- #该片段来自于http://www.codesnippet.cn/detail/080620133928.html
来源: http://www.codesnippet.cn/detail/080620133928.html