Write a function in Python to read lines from a text file "notes.txt", to find and display the
occurrence of the word "the".
For example: If the content of the file is:
"India is the fastest-growing economy. India is looking for more investments around the globe. The
whole world is looking at India as a great market. Most of the Indians can foresee the heights that
India is capable of reaching."
The output should be 5
Answers
Answered by
2
def count(file, word):
from os import path, sys
from collections import Counter
if path.isfile("notes.txt"):
with open("notes.txt", 'r') as notes:
return Counter(notes.read( ).lower( ).split( )).get(word)
else:
print("Some files seem to be missing...")
sys.exit( )
Answered by
0
Answer:
def number_of_words(fname)
with open ('file.txt') as f
count =0
d= f.read
words = d.split()
for x in words :
if x =='the'
count = count +1
print (count)
number_of_words('file.txt')
Similar questions
Math,
2 months ago
English,
2 months ago
English,
5 months ago
Social Sciences,
5 months ago
Political Science,
10 months ago