Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize two folders using python script

Currently, I am working on a project in which am synchronizing two folders. My folders in the following example names ad Folder_1 as source and Folder_2 as destination I want to do the following things.

  1. If files which are present in Folder_1 are not present in Folder_2, copy the files from folder_1 to Folder_2 and Vice Versa.
  2. If I rename any file in either folder, it gets updated in the other folder instead of copying a new file with the updated name.
  3. if I delete any file from any folder, it should get deleted from the other folder as well.

I have done half the part of point one in which I am able to copy the files from Folder_1 to Folder_2. Send part where I could be able to copy files from Folder_2 to folder_1 is still remaining.

Following is my code

import os, shutil
path = 'C:/Users/saqibshakeel035/Desktop/Folder_1/'
copyto = 'C:/Users/saqibshakeel035/Desktop/Folder_2/'

files =os.listdir(path)
files.sort()
for f in files:
        src = path+f
        dst = copyto+f
        try:
                if os.stat(src).st_mtime < os.stat(dst).st_mtime:
                        continue
        except OSError:
                        pass
                        shutil.copy(src,dst)#this is the case when our file in destination doesn't exist
                               =
print('Files copied from'+ path +'to' + copyto+ '!')

What can I amend or do so that I can synchronize both folders completely? Thanks in advance :)

like image 555
Saqib Shakeel Avatar asked Feb 14 '19 10:02

Saqib Shakeel


People also ask

How to synchronize threads in Python?

Synchronization in Python – Different Methods to Synchronize Threads 1 Lock Objects#N#A Lock object is the most basic synchronization primitive which is not owned by a particular thread when... 2 RLock Objects#N#A reentrant lock (RLock) is another synchronization primitive that may be acquired multiple times by... 3 Semaphores More ...

How do I sync two folders in Python?

The script is nice and simple, only two lines, copy and paste the following into either IDLE (installed with Python) or notepad and save as "DirectorySync.py": Make sure to change the two folders above with the two folders you wish to sync. The double backslash is required in the path name as the backslash is an escape character in Python.

How to merge multiple folders in Python?

This can be done using Python’s OS and Shutil module. Get the current directory and the list of the folders you want to merge. Loop through the list of folders and store their content in a list. Here, we have stored them in the dictionary so that we can have the name of the folder as a key and its content as a value list.

How do I run a sync without having Python installed?

In the dist folder is now a file called DirectorySync.exe, running this will perform the sync in the background. This file can be distributed as is to anyone and they can then run a sync without having to have python installed on the computer. To explain what is going on in the BOLD text which was typed in the command prompt:


1 Answers

(Not the same approach as yours but gets the work done as expected from your query)

Simple code using dirsync:

from dirsync import sync
source_path = '/Give/Source/Folder/Here'
target_path = '/Give/Target/Folder/Here'

sync(source_path, target_path, 'sync') #for syncing one way
sync(target_path, source_path, 'sync') #for syncing the opposite way

See documentation here for more options: dirsync - PyPI

You can, of course, add exception handling manually if you want.

like image 51
MPatel1 Avatar answered Sep 21 '22 12:09

MPatel1