Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果正则表达式匹配,则将文件路径添加到消息_Python_Windows - Fatal编程技术网

Python 如果正则表达式匹配,则将文件路径添加到消息

Python 如果正则表达式匹配,则将文件路径添加到消息,python,windows,Python,Windows,下面是我的Python脚本摘录,它读取E:\Data\Production目录下的所有日志文件,并查找某些正则表达式匹配项 errors, tr, warnings = [], [], [] rootdir = 'E:\\Data\\Production' for folder, dirs, files in os.walk(rootdir): for file in files: if file.endswith('.log'): fullp

下面是我的Python脚本摘录,它读取E:\Data\Production目录下的所有日志文件,并查找某些正则表达式匹配项

errors, tr, warnings = [], [], []

rootdir = 'E:\\Data\\Production'

for folder, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith('.log'):
            fullpath = os.path.join(folder, file)
            with open(fullpath, 'r') as f:
                for line in f:
                    for match in re.finditer(r'.*' + daysdate + '(?!.*[->OK].*).*[Ee]rror.*', line):
                        errors.append(match.group(0))
                    for match in re.finditer(r'.*' + daysdate + '.*(?!.*deployed)(?!.*complete.*)(?!.*There are no.*)(?!.*disabled.*).*TR\d{4}.*', line):
                        warnings.append(match.group(0))
                    for match in re.finditer(r'.*' + daysdate + '.*(?!.*deployed)(?!.*[->OK])(?!.*complete.*)(?!.*There are no.*)(?!.*disabled.*).*TR\d{4}.*', line):
                        tr.append(match.group(0))

if errors == []:
    errors.append("No errors found.")
[...]
sendmail():
    "Errors:\n\n%s\n" % "\n".join(map(str, errors)) +
    "\Faulty:\n\n%s\n" % "\n".join(map(str, tr)) +
    "\Warnings:\n\n%s\n" % "\n".join(map(str, warnings))
sendmail()
其输出为:

Errors:
No errors found.
Faulty:
<the entire line containing the matched regex>
Warnings:
No errors found.
我想做的是添加发现错误的日志文件的完整路径,因此它将改为:

Errors:
No errors found.
Faulty:
<the entire line containing the matched regex>
Error found in file: E:\Data\Production\qwer\asdf\zxcv.log
Warnings:
No warnings found.
我假设我需要以某种方式附加fullpath变量,但我不知道如何实现它。

我相信您需要

for match in re.finditer(r'.*' + daysdate + '.*(?!.*deployed)(?!.*[->OK])(?!.*complete.*)(?!.*There are no.*)(?!.*disabled.*).*TR\d{4}.*', line):
    tr.append(match.group(0) + "\n{0}".format(fullpath))