Python Watchdog 在启动时处理现有文件

sco*_*tyj 5 python queue watchdog python-watchdog

我有一个简单的 Watchdog 和 Queue 进程来监视目录中的文件。代码取自https://camcairns.github.io/python/2017/09/06/python_watchdog_jobs_queue.html

import time
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
from queue import Queue
from threading import Thread

dir_path = "/data"

def process_queue(q):

    while True:
        if not q.empty():
            event = q.get()
            print("New event %s" % event)

        time.sleep(5)


class FileWatchdog(PatternMatchingEventHandler):

    def __init__(self, queue, patterns):
        PatternMatchingEventHandler.__init__(self, patterns=patterns)
        self.queue = queue

    def process(self, event):
        self.queue.put(event)

    def on_created(self, event):
        self.process(event)


if __name__ == '__main__':

    watchdog_queue = Queue()

    worker = Thread(target=process_queue, args=(watchdog_queue,))
    worker.setDaemon(True)
    worker.start()

    event_handler = FileWatchdog(watchdog_queue, patterns="*.ini")
    observer = Observer()
    observer.schedule(event_handler, path=dir_path)
    observer.start()

    try:
        while True:
            time.sleep(2)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

Run Code Online (Sandbox Code Playgroud)

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

我试图创建一个 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)
Run Code Online (Sandbox Code Playgroud)

但它需要一个类型的对象(类'watchdog.events.FileCreatedEvent'),我不知道如何创建它。

或者,我可以在 Watchdog 文档(类 watchdog.utils.dirsnapshot.DirectorySnapshot)中看到,但我无法弄清楚如何运行它并将其添加到队列中。

关于如何在启动时将现有文件添加到队列的任何建议?

小智 5

这段代码应该可以实现您想要实现的目标。

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)
Run Code Online (Sandbox Code Playgroud)