Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to open all .txt and .log files in the current directory, search, and print the file the search was found
  2. fiLe = open(logfile, "r")
  3.       userString = raw_input("Enter a string name to search: ")
  4.       for line in fiLe.readlines():
  5.           if userString in line:
  6.              print line
  7.        
  8. import os
  9. directory = os.path.join("c:\","path")
  10. for root,files,dir in os.walk(directory):
  11.     for file in files:
  12.        if file.endswith(".log") or file.endswith(".txt"):
  13.            f=open(file, 'r')
  14.            for line in f:
  15.               if userstring in line:
  16.                  print "file: " + os.path.join(root,file)            
  17.                  break
  18.            f.close()