How Do You Write a Python Program to Parse a Log File and Return Lines with the Word “Failed”?

Problem scenario
You want to retrieve lines from a log file with the word "failed". How do you do this with Python?

Solution

Assuming the log file is named "auth.log", this will work:

log_reader = open('auth.log', 'r')
for line in log_reader:
  lower_line = line.lower()
  if lower_line.find("failed") != -1:
    print(line)

Leave a comment

Your email address will not be published. Required fields are marked *