Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import os
import time
import multiprocessing
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
    def on_created(self, event):
        print(f'event type: {event.event_type} path : {event.src_path}')

def monitor_folders(path, newPath):
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

folder1 = '/home/bilal/Videos/folder1'
folder2 = '/home/bilal/Videos/folder2'

if __name__ == "__main__":
    m1 = multiprocessing.Process(target=monitor_folders, args=(folder1, folder2))
    m2 = multiprocessing.Process(target=monitor_folders, args=(folder2, folder1))
    m1.start()
    m2.start()


What I have tried:

This code is monitoring the data of two paths. Both directory paths have the same number and the same name of subdirectories.

But I want to add something more to this code and that is if I created a file in a parent directory then that file should be copied to the second parent directory. And if I created a file in any child subdirectory then that file should also be copied to the second subdirectory path.

I used os.system(f"rsync -a {folder1}/ {folder2}") but that was only copying the parent directory.
Posted
Comments
Richard MacCutchan 6-Mar-21 11:55am
   
You need to capture the full path name of the source file, and use the content to build the pathname of the destination.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900