find the entries with the string "panic" in all of the log files in a directory in python

You can use the os and re modules to achieve this task. Here's an example:

main.py
import os
import re

# Define the directory path
path = 'path/to/log/files/'

# Loop through all the files in the directory
for filename in os.listdir(path):
    if filename.endswith('.log'):
        # Open the file and read its contents
        with open(os.path.join(path, filename), 'r') as file:
            lines = file.readlines()
            
            # Loop through all the lines and search for the string "panic"
            for line in lines:
                if re.search('panic', line):
                    print(f"Found 'panic' in file {filename}: {line}")
572 chars
18 lines

In this example, we're looking for all files with the .log extension in the specified path. Then, we're opening each file and reading its contents. For each file, we're looping through each line of the file and searching for the string "panic" using a regular expression. If we find a line with "panic", we print the filename and the line where the match was found.

gistlibby LogSnag