Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import os
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

def monitor_folders(folder_name, transfer):
    def on_created(event):
        print(f"Filesystem is Created in {folder_name}")
        os.system(f"rsync -a {folder_name} {transfer}")

    event_handler = FileSystemEventHandler()
    event_handler.on_created = on_created

    observer = Observer()
    observer.schedule(event_handler, folder_name, recursive=True)
    observer.start()
    try:
        print("Monitoring")
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        print("Done")
    observer.join()

if __name__ == "__main__":
    path = "/home/bilal/Videos/folder1/"
    newPath = "/home/bilal/Videos/folder2/"
    monitor_multiple_folders(path, newPath)


What I have tried:

In this code, I am monitoring a path, and when a new file is created, the file should transfer to the newPath. The code is working fine but my question is that, how can I monitor all the subdirectories that are present in the parent directory?

I used LoggingEventHandler to watch subdirectories but it was not flexible to give me a way to transfer files to another path.
Posted
Updated 5-Mar-21 22:56pm

1 solution

Use the recursive option as described at watchdog · PyPI[^].
 
Share this answer
 

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