To write a python program for getting the word count from a text.
PC Anaconda - Python 3.7
Create a file and add some content into it.
Open file using with keyword/built-in function in read mode.
Use read() to read the contents of the file.
Split the lines using split().
Iterate the list and increment the count
Print the output.
To write a program for getting the word count from a file.
Developed by:Harini V
RegisterNumber:22004214
def wordcount():
cnt = 0
with open("MyFile.txt","r") as fp:
data = fp.read()
for line in data.split():
cnt += 1
print("The number of words in the given file is:",cnt)
wordcount()
Thus the program is written to find the word count from a text.