
Untitled
By: a guest on
Jun 20th, 2012 | syntax:
None | size: 0.67 KB | hits: 7 | expires: Never
How to open all .txt and .log files in the current directory, search, and print the file the search was found
fiLe = open(logfile, "r")
userString = raw_input("Enter a string name to search: ")
for line in fiLe.readlines():
if userString in line:
print line
import os
directory = os.path.join("c:\","path")
for root,files,dir in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(file, 'r')
for line in f:
if userstring in line:
print "file: " + os.path.join(root,file)
break
f.close()