How to synchronize two folders using python script
Asked Answered
C

1

17

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 :)

Christean answered 14/2, 2019 at 10:52 Comment(4)
How do you know if there is a file missing (case 1) or the file was deleted (case 3)?Brigittebriley
So what's the problem?Trudi
@Trudi problem is that I am not understanding that how can is use .os to delete the files from destination if I delete the file from source folder..??Christean
@Brigittebriley my code { files =os.listdir(path) } list all the files present in a directory. Say Folder one. "Try" checks that the file saved in Destination folder has latest time and if this is true it goes to the "except " where it says to copy the file to the destination folderChristean
A
29

(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.

Agamic answered 27/8, 2019 at 17:49 Comment(6)
Can we use dirsync to sync two folders lying on two different host machines?Stumer
@Stumer Yes , it can. Just you need to make sure you've got the paths right. In Mac connected to local server, /Volumes/SomeRemoteFolder/Subfolder works fine after connecting server through Go --> Connect to Server ( Command + K ).Agamic
This is just majestic! sorry to comment it but finally found something useful that can be done in windows when not having administration privileges.Pavo
From pypi.org/project/dirsync it seems like there is a 2-way option to make the trick in one command : --twowayCl
Thank you so much for this amazing sharing M. Patel.Merat
Is possible to implement a progressbar or something to show the process progress?Solemn

© 2022 - 2024 — McMap. All rights reserved.