Python 备份指定目录文件

来源:互联网 发布:邓小平网络纪念馆留言 编辑:程序博客网 时间:2024/06/06 04:21
      1#!/usr/bin/python
      2import os
      3import time
      4source=['/home/wanghao/byte','/home/wanghao/bin']
      5target_dir = '/home/wanghao/'
      6today = target_dir + time.strftime('%Y%m%d')
      7 now= time.strftime('%H%M%S')
      8comment = raw_input('Enter acomment-->')
      9 iflen(comment) == 0:
     10    target=today + so.sep + now +'.tar.gz'
     11else:
     12    target = today + os.sep + now+ '_' + comment.replace('','') + '.tar       .gz'
     13 ifnot os.path.exists(today):
     14    os.mkdir(today)
     15    print 'Sucessful createddirectory',today
     16tar_command = "tar -zcvf '%s' %s" % (target,' '.join(source))
     17 ifos.system(tar_command)==0:
     18    print 'Successful backupto',target
     19else:
     20    print 'Backup FAILED'


文件夹为当前日期,文件名为当前日期
0 0