I have a few .txt log files and I am looking for a way to loop through all of them and look for a specific string inside. Once found all the results to be printed in a new .txt file. So far I've managed to create this:
import os
stringToMatch = 'dataToLookFor'
matchedLine = ''
with open('Logs/mylogs.txt', 'r') as file:
for line in file:
if stringToMatch in line:
matchedLine = line
continue
with open('Logs/results.txt', 'w') as file:
file.write(matchedLine)
With this I am able to find results 1 by 1 but only in the file mylogs.txt. Any help is very much appreciated.