File Handling 13: Count number of occurances of a word in a file

# Count number of times a word exists in a file

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

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

if not Line:
break

counter += Line.count(search_str)

if(counter):
print(search_str, "present in file", counter , "times")
else:
print(search_str, "not present in file")

fs.close()

No comments:

Post a Comment

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

Anu