如何正确运行看门狗的RegexMatchingEventHandler? [英] How to run the RegexMatchingEventHandler of Watchdog correctly?
问题描述
我正在为GameApi开发一个小型工具.该Api可与.log文件一起使用.它们在特定位置提供.如果要使用PatternMatchingEventHandler,我想与看门狗一起观察此位置,并且工作正常.但是,如果我使用RegexMatchingEventHandler,它将失败.我想使用Regex,因为那里有很多.log文件,我只想从今天开始检查文件.
I'm working on a small tool for a GameApi. This Api works with .log-files. They are provided in a specific location. I want to observe this location with with watchdog and it is working fine, if I use the PatternMatchingEventHandler. But if I use the RegexMatchingEventHandler it fails. I want to use Regex because there a many .log-files and I only want to check the files from today.
扩展:我将Watchdog与以下功能配合使用:
Extension: I use Watchdog with the functions:
on_created
on_deleted
on_moved
on_modified
此网站显示了我正在使用的代码: https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/
This Website shows the code I am using: https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/
我用re正常测试了Regex函数.这样做绝对好.但是,即使我尝试使用正则表达式条目:['\ w + .log']也无法正常工作.
I tested my Regex function normaly with re. This is doing absolutly fine. But even if I try the Regex Entry: ['\w+.log'] it did not work.
我为您提供我的正则表达式以了解我想做什么:
I provide you my Regex to understand what I want to do:
regexes = ["^Journal\.190901\d{6}\.\d{2}\.log$"]
每次更改一个.log文件时,我都希望收到一条消息,但这仅在使用PatternMatchingEventHAndle时发生
I expected a message everytime I change one of my .log-files but this is only happend i f i use the PatternMatchingEventHAndle
现在,我向您介绍我的最小示例:
Now I present you you my minimal Example:
import time
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
if __name__ == "__main__":
regexes = ["^Journal\.190901\d{6}\.\d{2}\.log$"]
ignore_regexes= []
ignore_directories = False
case_sensitive = True
my_event_handler = RegexMatchingEventHandler(regexes,ignore_regexes,ignore_directories,case_sensitive)
def on_modified(event):
print(f"hey buddy, {event.src_path} has been modified")
my_event_handler.on_modified = on_modified
# Observer
path = "."
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()
推荐答案
这只是事件处理程序返回的路径的模式,它从path
中指定的文件夹根目录的路径开始( c4>在您的示例中).
This is just about the pattern of the path returned by the event handler, it begins by the path to the root of the folder specified in path
(path = "."
in your example) .
-
它可以帮助检查返回的路径的任何模式,并确切地检查您需要什么:
It can help to inspect any patterns of the paths returned, and check what you need exactly:
regexes = ['.+'] # will match everything
使用path = "."
跟踪当前目录中的文件:
To track files in the current directory with path = "."
:
# linux (example path : ./Journal[...].log)
regexes = ['^\./Journal\.190901\d{6}\.\d{2}\.log$']
# windows (example path : .\Journal[...].log)
regexes = ["^\.\\\\Journal\.190901\d{6}\.\d{2}\.log$"]
# both
regexes = ["^\.(/|\\\\)Journal\.190901\d{6}\.\d{2}\.log$"]
,如果您定义一个名为logs
的子文件夹,并指定path = "logs"
if you define a subfolder named logs
and you specify the root like path = "logs"
# (example path : logs/Journal[...].log or logs\Journal[...].log
regexes = ['^logs(/|\\\\)logs/Journal\.190901\d{6}\.\d{2}\.log$']
这篇关于如何正确运行看门狗的RegexMatchingEventHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!