python备份文件+时间函数

# encoding: utf8
# Filename: backup_ver_0.2.py
# Author:   JDI
# Date  :   2012-12-02 18:41:14

import os
import time
import datetime

# 1. The files and directories to be backed up are specified in a list
source = [r'e:\doc', r'e:\media']
# using Windows

# 2. The backup must be stored in a main backup directory
target_dir = r'f:\backup'  # Remember to change this to what you will be using

# 3. The files are backed up into a zip file
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 5. We use the zip command
zip_command = "7z a %s %s" % (target, ' '.join(source))

# before run the command
starttime = datetime.datetime.now()

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

# after run the command, write th date
endtime = datetime.datetime.now()

countTime = (endtime - starttime).seconds

hour = time.strftime('%H', time.gmtime(countTime))
minutes = time.strftime('%M', time.gmtime(countTime))
secs = time.strftime('%S', time.gmtime(countTime))

strCountTime = u"%s时%s分%s秒" % (hour, minutes, secs)

print u"一共用时:%s" % (strCountTime) 


来自《简明python教程》的备份脚本的修改
加入了备份的用时计算

你可能感兴趣的:(python,时间函数,文件备份)