Python看门狗类型错误:“bool”对象不可迭代
Python Watchdog TypeError:“ bool” object is not iterable
我正在编写一个脚本来检查给定 CSV 文件中的任何修改,并尝试使用看门狗来执行此操作。按照 this 站点的(几乎相同的)示例,我有以下代码:
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()
当我将文件或目录添加到 watchfolder 时,出现类型错误:'bool' 对象不可迭代。这是回溯:
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
我尝试将 go_recursively 变量的类型更改为 str 和 tuple,但我得到了同样的错误。我还尝试将递归参数直接设置为 True 并得到相同的错误。即使我尝试 google 任何关于此错误的相关信息,但发现 none。非常感谢您的帮助!
问题是 ignore_directories
是 PatternMatchingEventHandler
的第三个参数,而不是第二个。您将该值作为 ignore_patterns
传递,它应该是一个列表。您可以提供缺少的位置参数:
my_event_handler = PatternMatchingEventHandler(patterns, None, ignore_directories, case_sensitive)
或者去关键字:
my_event_handler = PatternMatchingEventHandler(patterns, ignore_directories=ignore_directories, case_sensitive=case_sensitive)
我正在编写一个脚本来检查给定 CSV 文件中的任何修改,并尝试使用看门狗来执行此操作。按照 this 站点的(几乎相同的)示例,我有以下代码:
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()
当我将文件或目录添加到 watchfolder 时,出现类型错误:'bool' 对象不可迭代。这是回溯:
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
我尝试将 go_recursively 变量的类型更改为 str 和 tuple,但我得到了同样的错误。我还尝试将递归参数直接设置为 True 并得到相同的错误。即使我尝试 google 任何关于此错误的相关信息,但发现 none。非常感谢您的帮助!
问题是 ignore_directories
是 PatternMatchingEventHandler
的第三个参数,而不是第二个。您将该值作为 ignore_patterns
传递,它应该是一个列表。您可以提供缺少的位置参数:
my_event_handler = PatternMatchingEventHandler(patterns, None, ignore_directories, case_sensitive)
或者去关键字:
my_event_handler = PatternMatchingEventHandler(patterns, ignore_directories=ignore_directories, case_sensitive=case_sensitive)