You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Watchdog报错OSError: [WinError 87]参数不正确求助

解决Watchdog在Windows Server 2012上的WinError 87参数错误问题

我之前在Windows Server 2012环境下用Watchdog做文件监控时,也碰到过一模一样的OSError: [WinError 87] The parameter is incorrect错误,折腾了好几个方案才搞定,给你分享几个可行的解决思路:

  • 优先升级Watchdog版本:你用的0.8.3是2017年的老版本了,对Windows Server 2012的底层API支持有已知bug。直接升级到较新的稳定版,比如0.10.x系列,命令如下:

    pip install --upgrade watchdog
    

    我当时升级到0.10.6之后,这个参数错误直接就消失了,这是最省心的解决方案。

  • 检查监控路径的合法性:Windows系统对路径有严格限制,以下情况都可能触发WinError 87:

    • 路径长度超过260字符(Windows传统路径限制),可以换成短路径格式(比如把C:\Program Files\My Long Project Folder改成C:\PROGRA~1\MYLONG~1);
    • 路径包含空格、中文或其他非ASCII特殊字符,尽量换成纯英文无空格的路径;
    • 路径使用了相对路径但当前工作目录不对,建议换成绝对路径试试。
  • 替换Observer类型作为临时 workaround:如果暂时没法升级Watchdog,可以试试用PollingObserver或者ThreadPoolObserver替代默认的Observer。虽然PollingObserver是轮询模式,但能绕开Windows底层API的参数问题:

    from watchdog.observers.polling import PollingObserver
    from watchdog.events import FileSystemEventHandler
    import time
    
    class MyHandler(FileSystemEventHandler):
        def on_modified(self, event):
            # 你的处理逻辑
            print(f"文件变更: {event.src_path}")
    
    if __name__ == "__main__":
        path = r"C:\your\monitor\path"
        event_handler = MyHandler()
        observer = PollingObserver()  # 替换默认Observer
        observer.schedule(event_handler, path, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
    
  • 检查运行权限:Windows Server 2012的权限管控比较严格,试试用管理员身份运行你的Python脚本,或者给监控目录添加当前用户的完全控制权限,避免因权限不足导致的API调用失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:49:08