admin Site Admin
Joined: 28 Feb 2006 Posts: 24
|
Posted: Sat Oct 04, 2008 3:39 am Post subject: Search a file in python |
|
|
| Code: |
filename = "/tmp/test"
search_word = "blah"
file = open(filename)
line_num = 0
found = 0
for l in file.readlines() :
line_num += 1
index = l.find(search_word)
if index >= 0 :
print ("Found '%s' at line %d, character %d" %
(search_word, line_num, index))
found +=1
print "Found %d instances of '%s' in %s." % (found, search_word, filename)
|
|
|