首页  ·  知识 ·  编程语言
python写的目录同步小程序
佚名  本站原创    编辑:dezai  图片来源:网络
# -*- coding: utf-8 -*- #目录同步工 #使用方法: #SynchRoot sourcepath destinationpath&

# -*- coding: utf-8 -*-   
#目录同步工  
#使用方法:  
#SynchRoot sourcepath destinationpath  
import os  
import sys  
import shutil  
import getopt  
#是否显示操作  
_verbos = 0 
#检查源目录的文件及文件夹  
def SynchPath(src, dst):  
    #如果目录不存在,则创建目录  
    if not os.path.exists(dst):  
        os.mkdir(dst)      
    names = os.listdir(src)  
    for name in names:  
        srcname = os.path.join(src, name)  
        dstname = os.path.join(dst, name)  
        try:  
            if os.path.isdir(srcname):    #目录  
                #如果目录不存在,则创建目录  
                if not os.path.exists(dstname):  
                    os.mkdir(dstname)  
                SynchPath(srcname, dstname)  
            elif os.path.isfile(srcname):  
                #查看目的目录下是否有这个文件,如果没有,则拷过去  
                if not os.path.exists(dstname):  
                    if _verbos == 1:  
                        print "copying " + srcname + " to " + dstname  
                    shutil.copy2(srcname, dstname)  
        except (IOError, os.error), why:  
            if _verbos == 1:  
                print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))  
def usage():  
    print ''''' 
    usage: python SynchRoot.py -s srcpath -d dstpath -v  
    sample: python SynchRoot.py -s E:\\重要 -d E:\\test 
    '''   
def Main(argv):  
    srcpath = 'E:\\重要' 
    dstpath = 'E:\\test' 
    try:                                  
        opts, args = getopt.getopt(argv, "hs:d:v", ["help", "source", "target", "verbos"])  
    except getopt.GetoptError:            
        usage()                           
        sys.exit(2)  
          
    for opt, arg in opts:  
        if opt in ("-h", "--help"):        
            usage()                       
            sys.exit()                                     
        elif opt in ("-s", "--source"):   
            srcpath = arg  
        elif opt in ("-d", "--target"):   
            dstpath = arg  
        elif opt in ("-v", "--verbos"):                  
            global _verbos                 
            _verbos = 1   
      
    if not os.path.exists(srcpath):  
        print "错误:源路径必须存在!" 
    else:  
        print "正在同步: " + srcpath + " >>>>>>> " + dstpath  
        SynchPath(srcpath, dstpath)  
        print "已完成同步" 
          
 
#执行操作  
Main(sys.argv[1:]) 
# -*- coding: utf-8 -*-
#目录同步工
#使用方法:
#SynchRoot sourcepath destinationpath
import os
import sys
import shutil
import getopt
#是否显示操作
_verbos = 0
#检查源目录的文件及文件夹
def SynchPath(src, dst):
    #如果目录不存在,则创建目录
    if not os.path.exists(dst):
        os.mkdir(dst)   
    names = os.listdir(src)
    for name in names:
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if os.path.isdir(srcname):    #目录
                #如果目录不存在,则创建目录
                if not os.path.exists(dstname):
                    os.mkdir(dstname)
                SynchPath(srcname, dstname)
            elif os.path.isfile(srcname):
                #查看目的目录下是否有这个文件,如果没有,则拷过去
                if not os.path.exists(dstname):
                    if _verbos == 1:
                        print "copying " + srcname + " to " + dstname
                    shutil.copy2(srcname, dstname)
        except (IOError, os.error), why:
            if _verbos == 1:
                print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
def usage():
    print '''
    usage: python SynchRoot.py -s srcpath -d dstpath -v
    sample: python SynchRoot.py -s E:\\重要 -d E:\\test
    '''
def Main(argv):
    srcpath = 'E:\\重要'
    dstpath = 'E:\\test'
    try:                               
        opts, args = getopt.getopt(argv, "hs:d:v", ["help", "source", "target", "verbos"])
    except getopt.GetoptError:         
        usage()                        
        sys.exit(2)
       
    for opt, arg in opts:
        if opt in ("-h", "--help"):     
            usage()                    
            sys.exit()                                  
        elif opt in ("-s", "--source"):
            srcpath = arg
        elif opt in ("-d", "--target"):
            dstpath = arg
        elif opt in ("-v", "--verbos"):               
            global _verbos              
            _verbos = 1
   
    if not os.path.exists(srcpath):
        print "错误:源路径必须存在!"
    else:
        print "正在同步: " + srcpath + " >>>>>>> " + dstpath
        SynchPath(srcpath, dstpath)
        print "已完成同步"
       

#执行操作
Main(sys.argv[1:])

本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的