前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python同步文件

Python同步文件

作者头像
py3study
发布2020-01-06 18:05:52
9990
发布2020-01-06 18:05:52
举报
文章被收录于专栏:python3python3

最近在做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

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

简单说明下脚本:

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档