import re def getAllLinesWithMatchingSentence(filePath,sentence): """ Prints all the lines that has sentence in it filePath : str path of a file to search worked for .txt , .log files sentence: str string to search """ with open(filePath,'r') as f: for line in f: match = re.search(r"{}".format(sentence),line) if match: print(line) # Following function prints all the lines in a filePath that matches given sentence getAllLinesWithMatchingSentence( filePath= 'files/log1.log', sentence = "dataframe of shape")