I am trying to build on example in another question filter directory in python
and trying to add and return the line found along with filename in which it is found. Here is the code
import os
searchdir = r'C:\Python27\mycode'
searchstring = 'import sys'
def found_in_file(fname, searchstring):
with open(fname) as infp:
for line in infp:
if searchstring in line:
return True, line
return False
with open('found.txt', 'w') as outfp:
count = 0
search_count = 0
for root, dirs, files in os.walk(searchdir):
for name in files:
count += 1
full_name = os.path.join(root, name)
b_found,line = found_in_file(full_name, searchstring)
if b_found:
outfp.write(full_name + '\n')
outfp.writ(line+'\n')
search_count += 1
print 'total number of files found %d' % count
print 'number of files with search string %d' % search_count
I am receiving error
Traceback (most recent call last):
File "C:/Python27/mycode/fsearch", line 20, in <module>
b_found,line = found_in_file(full_name, searchstring)
TypeError: 'bool' object is not iterable
any advise? what am I doing wrong?