I'm very new to Python and I'm trying to solve the following problem. I want to iterate a List with words and lookup it in a file and if found return me the line number where the words are found.
files = glob.iglob(os.path.join(in_dir, '*.*'))
List = ['word1', 'word2',...'wordn']
for index, word in enumerate(List):
print(index, word)
for item in files: # Iterate all files in Input directory
filename = os.path.basename(item) # Copy the filename without the path
print('file: {0}'.format(filename))
with open(item, 'r') as f:
for num, line in enumerate(f, 0):
if word in line:
print('num: {0}, line: {1}'.format(num, line))
But it doesn't seem to work, it only searched the first value o the list. What I want to do is to search all the List values in every file.