Searching for multiple strings in a file by using a list of strings

Viewed 4803

I am attempting to somehow search for multiple strings and perform a certain action when a certain string is found. Is it possible to provide a list of strings and go through the file searching for any of the strings that are present in that list?

list_of_strings_to_search_for = ['string_1', 'string_2', 'string_3']

I'm currently doing it one-by-one, indicating every string I want to search for in a new if-elif-else statement, like so:

with open(logPath) as file:
    for line in file:
        if 'string_1' in line:
            #do_something_1
        elif 'string_2' in line:
            #do_something_2
        elif 'string_3' in line:
            #do_something_3
        else:
            return True

I have tried passing the list itself, however, the "if x in line" is expecting a single string, and not a list. What is a worthy solution for such a thing?

Thank you.

3 Answers
Related