File handling 14: display line containing word in file

# Search if a word exists in a file.
# Display all lines in which the word exists.

fs = open("ipt.txt", "r")
search_str = input("Enter string to search : ").strip().lower()
Flag = False

while(True):
Line = fs.readline().strip().lower()

if not Line:
break

if (search_str in Line):
Flag = True
print(Line)

if(Flag==False):
print(search_str, "not present in file")

fs.close()

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu