Reputation: 1828
I am using watchdog
in Python
to monitor realtime whenever a file is created or deleted.
Following examples, I tried with the following:
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_created(self, event):
print("File is created!")
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='C:/daten/dog.txt', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Of course, the file path='C:/daten/dog.txt'
does not exist when this script begins to run. But I still get error messages as
FileNotFoundError: [WinError 2] The system cannot find the file specified.
Why it's telling me it cannot find the file specified at the first place. I need it running to watch for the creation of the file after all.
Update:
Now I understand that watchdog
is for monitoring a folder rather than a file.
Is there a similar package for monitoring a file or is it just better done by while
and sleep
statements together?
Upvotes: 0
Views: 2830
Reputation: 42207
Why it's telling me it cannot find the file specified at the first place.
Because the path you give to watchdog is where it's going to hook itself to listen for events.
You can't watch a file and expect its creation event to be recorded. File creation events are posted on the parent directory, so that is what you should be watching.
In fact I don't know that watching a specific file makes any sense with watchdog, its tagline is
Directory monitoring made easy with
Upvotes: 2