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

original = []
class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        global original
        originalFile = event.src_path.split('/')[-1]
        original.append(originalFile)
        if Path(event.src_path).parent.samefile(fullPath):
            return True
        return False

    while True:
        if on_modified:
            global original
            print(original)
            print("Main path is modified")
            break

    while False:
        if on_modified:
            global original
            print(original)
            print("Sub path is modified")
            break

def monitorFolders(path):
    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()

fullPath = "/home/bilal/Documents/Projects/BMP/Monitor/folder1/"

if __name__ == "__main__":
    m1 = monitorFolders(fullPath)
    m1.start()
    m1.join()


What I have tried:

In this code, I am facing two problems.

The first problem is that t gives me the message multiple times. I want to show the message just once. That's why I used a while loop with a break. The while loop works for True but I want to make the while loop work for both True and False.

The second problem is that I am using on_modified inside the while loop but I don't know how to call this function to make it work properly.
Posted
Updated 19-Apr-21 20:03pm
v4
Comments
Richard MacCutchan 20-Apr-21 4:04am    
That code makes no sense. you have two while loops that will never get executed, and even if they did, while False: means, "do not run this loop".

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