Python Windows 桌面备份,python备份,[Python]代码#


[Python]代码

# -*- coding: cp936 -*-# Author :Jeff Yu# Function: backupprint '======================================================'print '# 作者: Jeff Yu                                        'print '# 版本: Version 1.0                                    'print '# 功能: 备份桌面文件(带注释)                             'print '# 说明: 请首先安装7z软件,配置7z的环境变量(Path)           'print '#       具体情况可以问Jeff Yu                            'print '======================================================'import osimport timeuser = 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 '成功备份到:', targetelse:    print 'Backup FAILED'raw_input('请按回车键退出 --> ')

评论关闭