Python Watchdog 在启动时处理现有文件 [英] Python Watchdog process existing files on startup
问题描述
我有一个简单的看门狗和队列进程来监视目录中的文件.代码取自 https://camcairns.github.io/python/2017/09/06/python_watchdog_jobs_queue.html
I have a simple Watchdog and Queue process to monitor files in a directory. Code taken from 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()
一旦进程运行,新文件就会被正确处理.但是,如果我重新启动进程并且目录中已经存在一个文件,则它会被忽略.
我试图创建一个字典来添加到队列中
I have tried to create a dict to add to the queue
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"),我不知道如何创建它.
but it's expecting an object of type (class 'watchdog.events.FileCreatedEvent') and I can't work out how to create this.
或者,我可以在 Watchdog 文档(类 watchdog.utils.dirsnapshot.DirectorySnapshot)中看到,但我不知道如何运行它并将其添加到队列中.
Alternatively I can see in the Watchdog documentation (class watchdog.utils.dirsnapshot.DirectorySnapshot) but I cannot work out how to run this and add it to the queue.
关于如何在启动时将现有文件添加到队列中的任何建议?
推荐答案
此代码应该可以完成您想要实现的目标.
This code should do what you are trying to achieve.
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)
这篇关于Python Watchdog 在启动时处理现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!