I am learning how to read txt files and find something in them. The example below outputs the entire txt file. I am trying to get it to print out "found it" when it finds the word "thanks" in the txt file. Where am I wrong?
This is the txt file I am reading:
this is a
demo file
for exercises
thanks
bye
This is the code I have written:
f = open("demo.txt", "r")
print(f.readline())
print(f.readline())
for word in f:
print(word)
if word == "thanks":
print("found it")
This is the output:
this is a
demo file
for exercises
thanks
bye
Process finished with exit code 0
thanks\nmeaning it contains newline. Change==intoin.word.strip()to remove trailing space and new line character.