为什么python watchdog无法监控某些文件夹?

问题描述 投票:0回答:1

我正在尝试使用 python 中的“watchdog”包来监视文件夹的更改。 以下代码是直接从看门狗文档中复制的,并且运行良好。

import sys
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path =  '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=False)
    observer.start()
    try:
        while observer.is_alive():
            observer.join(1)
    finally:
        observer.stop()
        observer.join()

但是,当我尝试更改“path”变量时,代码可能无法工作。 例如,如果

path='./build/'
,我可以按预期看到 './build/' 文件夹中的更改,但如果
path='./build/result/'
,无论我对相应文件夹进行什么更改,终端都不会打印任何内容。

我不明白为什么,我必须寻求帮助。

我通过 WSL2 使用 ubuntu-20.04LTS。

python watchdog
1个回答
0
投票

不确定上面的代码是否已更改(并且意识到这已经晚了几年!),但乍一看,如果您想监视子文件夹中的更改,

recursive
不必设置为
True

默认情况下,watchdog.observers.Observer 实例不会监控 子目录。通过在调用中传递 recursive=True watchdog.observers.Observer.schedule() 监控整个目录 树木有保障。

https://python-watchdog.readthedocs.io/en/stable/quickstart.html

© www.soinside.com 2019 - 2024. All rights reserved.