Skip to content

Commit f5d28fc

Browse files
authored
Count the Occurrences of a Word in a Text File.py
The program takes a word from the user and counts the number of occurrences of that word in a file.
1 parent 7d5818c commit f5d28fc

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fname = input("Enter file name: ")
2+
word=input("Enter word to be searched:")
3+
k = 0
4+
5+
with open(fname, 'r') as f:
6+
for line in f:
7+
words = line.split()
8+
for i in words:
9+
if(i==word):
10+
k=k+1
11+
print("Occurrences of the word:")
12+
print(k)

0 commit comments

Comments
 (0)