I am writing a script to check for any modifications in a given CSV file and am trying to use watchdog to do this. Following the (almost identical) example from this site, I have the following code:
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
def on_deleted(event):
print(f"what the f**k! Someone deleted {event.src_path}!")
def on_modified(event):
print(f"hey buddy, {event.src_path} has been modified")
if __name__ == "__main__":
patterns = ["meetingschedule.csv"]
ignore_directories = True
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns, ignore_directories, case_sensitive)
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
path = "E:\\COPY\\APPs\\ZoomAutoLog\\Zoommeeting-main\\Ext_Tools\\"
my_observer = Observer()
my_observer.schedule(my_event_handler, path)
my_observer.start()
print('Watchdog is Monitoring now...')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print('Stopping Watchdog...')
my_observer.stop()
my_observer.join()
When I add a file or directory to watchfolder, I get a TypeError: 'bool' object is not iterable. Here's the traceback:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Program Files\Python39\lib\site-packages\watchdog\observers\api.py", line 199, in run
self.dispatch_events(self.event_queue, self.timeout)
File "C:\Program Files\Python39\lib\site-packages\watchdog\observers\api.py", line 372, in dispatch_events
handler.dispatch(event)
File "C:\Program Files\Python39\lib\site-packages\watchdog\events.py", line 399, in dispatch
if match_any_paths(paths,
File "C:\Program Files\Python39\lib\site-packages\watchdog\utils\patterns.py", line 85, in
match_any_paths
if _match_path(path, set(included), set(excluded), case_sensitive):
TypeError: 'bool' object is not iterable
I have tried changing the type of the go_recursively variable to str and tuple and I get the same error. I've also tried setting the recursive argument directly to True and get the same error. Even I tried to google any relevant info on this error but found none. I'd appreciate the help!
The problem is that ignore_directories
is the 3rd parameter to PatternMatchingEventHandler
, not the 2nd. You're passing that value as ignore_patterns
, which is expected to be a list. You can either supply the missing positional param:
my_event_handler = PatternMatchingEventHandler(patterns, None, ignore_directories, case_sensitive)
Or go keyword:
my_event_handler = PatternMatchingEventHandler(patterns, ignore_directories=ignore_directories, case_sensitive=case_sensitive)