python创建备份压缩程序(windows版)

上一篇 / 下一篇  2012-11-17 01:27:29 / 个人分类:python

# -*- coding: cp936 -*-
#Filename:backup_ver1.py
import os
import time
from zipfile import *
#压缩目录列表下所有文件
def back_zip(target,source):
    myzip = ZipFile(target,"w",ZIP_STORED)
    for i in range(0,len(source)):
        start = source[i].rfind(os.sep)+1
        for dirpath,dirs,files in os.walk(source[i]):
            for file in files:
                z_path = os.path.join(dirpath,file)
                myzip.write(z_path,z_path[start:])
        if i == len(source):
            myzip.close()
#压缩目录路径列表
source= [r'D:\work1',r'D:\work2']
#压缩保存目标路径
target_dir = 'D:\\backup\\'
#获取日期、时间
today = target_dir + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
#创建当天日期的备份目录
if not os.path.exists(today):
    os.mkdir(today)
    print '创建当天备份目录成功:',today
#输入压缩文件备注信息
comment = raw_input('输入您要加的备注信息 --> ')
if len(comment) == 0:
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now +'_' + \
             comment.replace('','')+'.zip'
#压缩文件
back_zip(target,source)


 


TAG:

 

评分:0

我来说两句

Open Toolbar