创建文件备份

原文链接:http://www.cnblogs.com/devtao/p/3397813.html

一个简单的例子,A byte of Python的例子,利用系统自带的命令创建某些文件的备份,放入指定的目录中

import os
import time

source = ['/home/dat/python/test1','/home/dat/python/test2']

target_dir = '/home/dat/python/'

today = target_dir+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
else:
    target = today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'

if not os.path.exists(today):
    os.mkdir(today)
    print 'successfully created directory',today

tar_command = 'tar -cvzf %s %s -X /home/dat/python/excludes.txt' % (target, ' '.join(source))

if os.system(tar_command)==0:
    print 'Success back to',target
else:
    print 'Back up Failed'

  

转载于:https://www.cnblogs.com/devtao/p/3397813.html

上一篇:前端用js写一个函数输出当前时间,格式为yyyy-mm-dd hh:mm:ss


下一篇:time与datetime模块