# -*-encoding:utf8 -*-
import os,time,getpass

#需要备份的目录

#[r' ']方括号的不是无辜存在的,如果不添加方括号,那么文字就会变成全角的样子,而不是简单的我们想要的。你可以在做zip的命令的时候print查看一下,传给zip的#命令到底是什么
source = [r'"D:\Python27\test"']
#定义备份的目标目录
target_d = r'D:\Python27\test\python'
print target_d

#获取当前备份脚本的用户
user = getpass.getuser()

print user

#定义今天的时间
today = target_d + time.strftime('%Y%m%d')
#获取当前时间
now = time.strftime('%H%M%S')
#定义在备份目录中需要注释的文字,可以选填
comment = raw_input('Please input :')
#判断用户有没有输入 备份注释文字
#如果没有,那么target的目录名定义为 备份目标目录+当前时间+用户+zip
if len(comment) == 0:
    target = today + '' +user + '.zip'
else:
    target = today + ' ' +user + comment + '.zip'

#zip的语法,可以自己在dos窗口下试验。
#zip -qr 静默方式打包,zip 目标目录 需要备份目录 通过用%来输入
zip = "zip -qr %s %s" % (target, ' '.join(source))
print zip
#判断zip运行是否成功
if os.system(zip) == 0:
    print 'OK'
else:
    print 'Failed'

 

最后添加上关于time模块的输出,来定制自己想要的zip备份目录

print (time.strftime('%Y-%m-%d %H:%M:%S'))
2012-04-10 14:52:09