Python 正则表达式匹配并等待新匹配

Python regex get match and wait until new match

基本上它是一个更新日志文件,所以当我的函数找到 next/latest 匹配项时,它会执行另一个函数 但我不希望它不断循环,只在找到最新更新时才工作

有什么方法可以 pause/wait 它直到文件更新并获得新的匹配项

try:
    with open(location, "r") as f:
        for lines in f.readlines():
            for match in re.findall(pattern, lines):
                matches.append(match)
                print(match)
                time.sleep(5)
except Exception:
    pass

open() 创建一个类似文件的对象,可以使用 readline() 对其进行迭代。如果您创建一个无限循环,您将不断读取其他程序附加到文件的任何新行。

with open(location, "r") as f:
    while True:
        line = f.readline()
        if line: #checks not None
            for match in re.findall(pattern, line)
            matches.append()
            do_work(matches) # call some other function that processes the matches that you have found