码迷,mamicode.com
首页 > 其他好文 > 详细

备份文件

时间:2014-10-27 22:31:37      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   sp   文件   div   on   

学习OS模块:

#!/usr/bin/python
import os
import time
import socket

def getsource():
    print "[Info]-Please input the absolute path of file or directory which you want to back up."
    spath = raw_input(>)
    if not os.path.exists(spath):
        print "[Error]-%s does not exsist.Try again."%spath
        exit()
    return spath

def gettarget():
    print "[Info]-Please input the absolute path of target directory."
    tpath = raw_input(>)
    if not os.path.exists(tpath):
        os.makedirs(tpath)
    return tpath

def backup(spath,tpath):
    hostname = socket.gethostname()
    filename = _.join([hostname,time.strftime(%m-%d-%Y),logs.tar.gz])
    filepath = tpath + os.sep + filename
    clist = [tar -zcvf, filepath, spath]
    command =  .join(clist)
    os.system(command)
    print "Backup successfully!"

if __name__ == __main__:
    source = getsource()
    target = gettarget()
    backup(source,target)

 

备份文件

标签:style   blog   color   os   ar   sp   文件   div   on   

原文地址:http://www.cnblogs.com/lostdonkey/p/4055182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!