You need to enable JavaScript to run this app. Python使用Watchdog报错TypeError:'bool' object is not iterable求助
优惠活动
大模型
产品
解决方案
定价
更多

Python使用Watchdog报错TypeError:'bool' object is not iterable求助

问题原因

你调用PatternMatchingEventHandler时参数传递顺序错误,导致布尔值被传入了要求可迭代对象的ignore_patterns参数位。
PatternMatchingEventHandler的构造函数参数顺序为:

PatternMatchingEventHandler(
    patterns=None,
    ignore_patterns=None,
    ignore_directories=False,
    case_sensitive=False
)

你漏传了第二个参数ignore_patterns,直接把ignore_directories = True作为第二个参数传入,相当于给ignore_patterns赋值为True,后续代码尝试把这个布尔值转为集合时自然触发'bool' object is not iterable报错。

修复方案

调整参数传递,补全ignore_patterns参数即可,有两种常用写法:

写法1:显式指定参数名(更稳妥,不受参数顺序影响)

把实例化事件处理器的代码修改为:

my_event_handler = PatternMatchingEventHandler(
    patterns=patterns,
    ignore_directories=ignore_directories,
    case_sensitive=case_sensitive
)

写法2:按参数顺序补全占位参数

给ignore_patterns传空值:

my_event_handler = PatternMatchingEventHandler(patterns, None, ignore_directories, case_sensitive)

内容的提问来源于stack exchange,提问作者PNxZEN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 21:36:04