How do you get your function to find the lines in the text file where the word occurs and print the corresponding line numbers?
I had to open a text file with the paragraph and then am supposed to search the paragraph for certain words and then print the specific line numbers for the words.
Here is what I have so far.
def index (filepath, keywords):
file = open(filepath)
files_lines = [line for line in file]
counter = 0
for line in files_lines:
counter += 1
if line.find(keywords) >= 0:
print(keywords, counter)
counter = 0
This is how the output should be
>>index('file.txt',['network', 'device', 'local'])
network 9
device 4
local 11
Note: Network, Device and local are the words im trying to search within the file and 9, 4, 11 are the line numbers on which these words occur.
I am getting an error that cannot convert list into str implicitly. Any help would be really appreciated. Thanks.