I have a file called con.dat. I want to read the content of this file to search for a pattern. If the pattern is found, it then prints the message to the console.
con.dat
- file: /app.log
pattern: "ERROR"
message: "Error in App"
I get the following error: AttributeError: 'list' object has no attribute 'keys'
my test.py code is as follows:
import yaml
import re
with open("con.dat","r") as f:
triggers = yaml.load(f)
for filename in triggers.keys():
with open(filename,"r") as f:
for line in f:
trigger_info = triggers(filename)
if re.search(trigger_info["pattern"],line):
print("match found in line{0} for pattern{1}")
format(line,trigger_info["pattern"])
I know my error related to triggers.keys(): but I am not sure the issue. I am new to python. Can someone guide me on how I should correct this error?
btw - my app.log file does contain a line with 'ERROR: blah blah blah' for testing
triggers.keys(): it seems thattriggersis not adictionarybut alist. Lists, do not have thekeys()attribute.for filename in triggers:, it should work