python windows环境下文档备份

#python 2.7
#Filename:backup.py
import os
import time
source = [r'C:Userszeng.shufangDesktopmessages_zh_CN-update.properties',r'C:Userszeng.shufangDesktopCapture.PNG']
target_dir=r'C:Userszeng.shufangDesktop'
today = target_dir+os.sep+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
comment=raw_input('Enter a comment-->')
if len(comment)==0:
    target = today+os.sep+now+'.rar'
else:
    target = today+os.sep+now+'_'+
        comment.replace(' ','_')+'.rar'
if not os.path.exists(today):
    os.mkdir(today)
    print 'Successfully created directory',today
zip_command = "Winrar A %s %s -r" %(target,' '.join(source))
if os.system(zip_command)==0:
    print 'Successful backup to',target
else:
    print 'backup failed'

注:由于Windows没有自带的压缩命令,需要把Winrar.exe文件复制到C:Windows目录下

原文地址:https://www.cnblogs.com/ly-radiata/p/6527672.html