Python看门狗在启动时处理现有文件

Python看门狗在启动时处理现有文件,python,queue,watchdog,python-watchdog,Python,Queue,Watchdog,Python Watchdog,我有一个简单的看门狗和队列进程来监视目录中的文件。 代码取自 进程运行后,新文件将正确处理。 但是,如果我重新启动进程,并且目录中已经存在一个文件,则该文件将被忽略。 我已尝试创建要添加到队列的dict for file in os.listdir(dir_path): if file.endswith(".ini"): file_path = os.path.join(dir_path, file) event = {

我有一个简单的看门狗和队列进程来监视目录中的文件。 代码取自

进程运行后,新文件将正确处理。 但是,如果我重新启动进程,并且目录中已经存在一个文件,则该文件将被忽略。

我已尝试创建要添加到队列的dict

    for file in os.listdir(dir_path):
        if file.endswith(".ini"):
             file_path = os.path.join(dir_path, file)
             event = {'event_type' : 'on_created', 'is_directory' : 'False', 'src_path' : file_path}
             watchdog_queue.put(event)
但是它需要一个类型为(类'watchdog.events.FileCreatedEvent')的对象,而我不知道如何创建它

或者,我可以在Watchdog文档(类Watchdog.utils.dirsnapshot.directorySapshot)中看到,但我无法确定如何运行该程序并将其添加到队列中


关于如何在启动时将现有文件添加到队列中,有什么建议吗?

这段代码应该满足您的要求

from watchdog.events import FileCreatedEvent

# Loop to get all files; dir_path is your lookup folder.

for file in os.listdir(dir_path):
    filename = os.path.join(dir_path, file)
    event = FileCreatedEvent(filename)
    watchdog_queue.put(event)

如果要排除子目录,并且如果os.path.isfile(文件名):在
filename=
行下
from watchdog.events import FileCreatedEvent

# Loop to get all files; dir_path is your lookup folder.

for file in os.listdir(dir_path):
    filename = os.path.join(dir_path, file)
    event = FileCreatedEvent(filename)
    watchdog_queue.put(event)