Nov-26-2019, 07:07 PM
I have some code to find an error message in an error log file. When the error is found, the very next block of text will be a path. I need to capture that path.
In other words, I am searching a text file for "reported errors in the". When that string is found in the file, I need the next block of text which will be something like /var/logs/[filename]. Not sure how to accomplish this.
My current code to find the error string is:
if strResult = 1:
Grab the next contiguous block of text after strWhat
Not sure if that is clear, but thanks for any help in advance.
In other words, I am searching a text file for "reported errors in the". When that string is found in the file, I need the next block of text which will be something like /var/logs/[filename]. Not sure how to accomplish this.
My current code to find the error string is:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# set variables
strFileExt=""
strFile=""
strError =""
# import required modules
import datetime
import os
import subprocess
now = datetime.datetime.now()
# for testing time format
#print (now.strftime("%m%d%y"))
strWhere = "/var/logs/error.log."+(now.strftime("%m%d%y"))
#print (strFileExt)
strWhat = "reported errors in the"
#print (strWhat)
#print (strWhere)
strResult = 0
# read file
try:
with open(strWhere, "r") as file:
lines = file.readlines()
except IOError:
strError = 10
except FileNotFoundError:
strError = 11
except Exception:
strError = 12
if (strError )>5:
print ( strError )
for line in lines:
line = line.strip()
if line.find( strWhat )!= -1:
strResult = strResult + 1
else: #do this when the loop is finished
# display results
print (strResult)So basically, if strResult = 1:
Grab the next contiguous block of text after strWhat
Not sure if that is clear, but thanks for any help in advance.
