-1

I'm building a little program that reads every line in a log file and if it finds a match it prints that line. The problem is, I have about 20 different log files and they all in the same folder. Is there a way I can parse through every single log file in a folder and print out the line that matches the searched word? Below is an example of what I have so far, but it prints nothing. The script needs to be able to incorporate readlines() and split()

What I have below doesn't work, but this is what I would expect it to look like. Any advice welcome.

  def Preview(): 
    path = ('C:Users/kev/Desktop/test/*.log')
    files = glob.glob(path)
    files.readlines()
    for line in files:
        if "test_word" in line:
            print line

   Preview()
user6534872
  • 63
  • 3
  • 9

1 Answers1

0

This is how your code should look:

def Preview(): 
    path = ('C:Users/kev/Desktop/test/*.log')
    files = glob.glob(path)
    for f in files:
        f = open(f)
        f = f.readlines():
        for line in f:
            if "test_word" in line:
            print line
        f.close()

Preview()
zipa
  • 27,316
  • 6
  • 40
  • 58
  • There were a few syntax issues in your code that I corrected, but it still doesn't work. Got the following error f = f.readlines() AttributeError: 'str' object has no attribute 'readlines' – user6534872 May 22 '17 at 20:35
  • Totally oversaw opening of file :) Please try the edit. – zipa May 22 '17 at 20:38
  • thank you!! - I also found this article which seemed to do the trick. (https://stackoverflow.com/questions/3110469/do-a-search-and-replace-across-all-files-in-a-folder-through-python) – user6534872 May 22 '17 at 20:59