Showing posts with label display all lines containing the search word in file. Show all posts
Showing posts with label display all lines containing the search word in file. Show all posts

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()