天天看點

Python同步檔案

最近在做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/test1/file1 to /tmp/testsyncfile/destinationfiles/test1/file1

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

│   ├── file

│   └── test1

│       ├── file1

│       └── test2

│           ├── file2

│           └── test3

8 directories, 6 files

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

簡單說明下腳本:

    簡單說明下腳本,腳本對源資料與目的資料做對比,有差異的檔案做替換和新增,對目錄做新增。但對于目的資料多餘的目錄和檔案,腳本不錯處理。希望使用腳本的人能根據自己的場景靈活運用。

    剛開始寫部落格,寫的比較草率,後續将分享更加生動的文章!