How to loop through .txt files and search for a specific string?

Viewed 1511

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.

2 Answers

You should have a look at the glob package. With this package you can find all *.txt files and do the needful operations:

import glob

files = glob.glob('Logs/*.txt')

will give you a list of files having txt file extension with in the Logs folder.


Having that, you can go through all the files and store matching lines to some output file:

string_to_match = 'dataToLookFor'

with open('Logs/results.txt', 'w') as outfile:
    for f in files:
        with open(f, 'r') as infile:
            for line in infile:
                if string_to_match in line:
                    outfile.write(f'{f}: {line}')

Not sure if this is exactly what it should look like but you can use os.listdir("Logs") to get all the files in that directory:

import os
stringToMatch = 'dataToLookFor'
matchedLine = ''

for file in os.listdir("Logs"):
    if ".txt" in file:
        with open('Logs/'+file, '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)
Related