python备份文件2 分类: python 20130315 15:16 233人阅读 评论(0) 收藏

#假设存在以下文件夹: D:\keys\miyao.将 keys文件夹中的内容备份

import os
import time
source=[r'D:\keys',r'\miyao']


target_dir=r'D:\\'


today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
comment=raw_input("enter a comment-->")


print 'comment :',comment
print len(comment)
if len(comment)==0:#如果条件成立,则target为 日期\时间.zip
    target=today+os.sep+now+'.zip'
else:
    target=today+os.sep+now+'_'+\
    comment.replace(' ','')+'.zip'
    print 'target :',target


print 'today:',today


if not os.path.exists(today):
    os.mkdir(today)
    print 'Successfully create directory',today


zip_command='zip -qr %s %s'%(target,''.join(source)) #zip -r 实现对文件夹的备份


print 'zip_command :',zip_command


if os.system(zip_command)==0:
    print 'Successful backup to',target


else:
    print 'Backup Failed'
原文地址:https://www.cnblogs.com/think1988/p/4628225.html