码迷,mamicode.com
首页 > 编程语言 > 详细

Python同步文件

时间:2017-05-05 15:33:36      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:开发   python   pyhon   

最近在做Python开发,研究了技术大牛写的脚本,在他的脚本上做了优化。优化脚本已在做过测试还是挺好用的,如果你觉得不错就直接拿到生产用吧。

先直接放代码出来:

---------------------------------------------------------------------------------------------------------

import re,shutil,os,sys,filecmp


diffFilesList = []

def commpare(dir1,dir2):

    cmpobs=filecmp.dircmp(dir1,dir2)

    dir1_only=cmpobs.left_only

    dir1_diff=cmpobs.diff_files

    [diffFilesList.append(os.path.join(os.path.abspath(dir1),a)) for a in dir1_diff]

    [diffFilesList.append(os.path.join(os.path.abspath(dir1),a)) for a in dir1_only]


    if len(cmpobs.common_dirs) > 0:

        for a in cmpobs.common_dirs:

            commpare(os.path.abspath(os.path.join(dir1,a)),os.path.abspath(os.path.join(dir2,a)))

    return diffFilesList


def main():

    if len(sys.argv) < 3:

        print "Plese use %s sourcedir backdir" % sys.argv[0]

        sys.exit()

    else:

        dir1=os.path.abspath(sys.argv[1])

        dir2=os.path.abspath(sys.argv[2])

    ifMakeDir = True

    while ifMakeDir:

        diffFilesList = []

        destinationFiles = []

        ifMakeDir=False

        sourceFiles=commpare(dir1,dir2)


        for a in sourceFiles:

            destinationFile=re.sub(dir1,dir2,a)

            destinationFiles.append(destinationFile)

            if os.path.isdir(a):

                if not os.path.exists(destinationFile):

                    os.makedirs(destinationFile)

                    print "Make dir %s" % destinationFile

                    ifMakeDir=True


    destinationFiles = []

    sourceFiles=[]

    sourceFiles=commpare(dir1,dir2)


    [ destinationFiles.append(re.sub(dir1,dir2,a)) for a in sourceFiles]

    for a,b in zip(sourceFiles,destinationFiles):

        if os.path.isfile(a):

            print "Copy file %s to %s" % (a,b)

            shutil.copyfile(a,b)


    #print sourceFiles,destinationFiles


if __name__ == ‘__main__‘:

    main()

---------------------------------------------------------------------------------------------------------

直接看效果:

[root@localhost tmp]# pwd

/tmp

[root@localhost tmp]# tree testsyncfile/

testsyncfile/

├── destinationfiles

└── sourcefiles

    ├── file

    └── test1

        ├── file1

        └── test2

            ├── file2

            └── test3

5 directories, 3 files


[root@localhost tmp]# python syncfile.py /tmp/testsyncfile/sourcefiles testsyncfile/destinationfiles/

Make dir /tmp/testsyncfile/destinationfiles/test1

Make dir /tmp/testsyncfile/destinationfiles/test1/test2

Make dir /tmp/testsyncfile/destinationfiles/test1/test2/test3

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

Copy file /tmp/testsyncfile/sourcefiles/file to /tmp/testsyncfile/destinationfiles/file

Copy file /tmp/testsyncfile/sourcefiles/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

Copy file /tmp/testsyncfile/sourcefiles/test1/test2/file2 to /tmp/testsyncfile/destinationfiles/test1/test2/file2

[root@localhost tmp]# tree testsyncfile/

testsyncfile/

├── destinationfiles

│   ├── file

│   └── test1

│       ├── file1

│       └── test2

│           ├── file2

│           └── test3

└── sourcefiles

    ├── file

    └── test1

        ├── file1

        └── test2

            ├── file2

            └── test3


8 directories, 6 files

----------------------------------------------------------------------------------------------------------

简单说明下脚本:

    简单说明下脚本,脚本对源数据与目的数据做对比,有差异的文件做替换和新增,对目录做新增。但对于目的数据多余的目录和文件,脚本不错处理。希望使用脚本的人能根据自己的场景灵活运用。

    刚开始写博客,写的比较草率,后续将分享更加生动的文章!

    对于这篇文章大家有疑问或者建议欢迎留言。

本文出自 “奔跑之马” 博客,请务必保留此出处http://5673807.blog.51cto.com/5663807/1922226

Python同步文件

标签:开发   python   pyhon   

原文地址:http://5673807.blog.51cto.com/5663807/1922226

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