Skip to main content

Posts

Showing posts from April, 2014

How to synchronized two folders with a Python script

That's what I wanted to do. So, I wrote my first Python (3) script. Don't hesitate to use or modified this code. It can synchronized two folders in two-way directions. It never deletes files or folders in its strategy. It only replicates older or missing files and folders from the two folders to synchronized.

Enjoy!

PS : Be cool with me, this is my first steps with Python


__author__ = 'Alexandre de Pellegrin'
import os
import shutil

srcDir = '/home/user/folder1'
dstDir = '/home/user/folder2'

def main():
    try:
        print("Begin sync")
        checkIfRootDirsExist(srcDir, dstDir)
        syncDirs(srcDir, dstDir)
        syncFiles(srcDir, dstDir)
        print("End sync with success")
    except Exception as e:
        print(e)
        print("End sync with failure!")




def checkIfRootDirsExist(rootDir1, rootDir2) :
    if (not os.path.exists(rootDir1) and not os.path.isdir(rootDir1)) :
        raise Exception(rootDir1 + " doesn't e…