To write a python program for getting the word count from a text.
PC Anaconda - Python 3.7
Get the file name as input from the user.
Open the file in read mode.
Use for loop to split the lines.
Store the split lines in a variable.
Add the number of lines and store it in a variable.
Print the stored variable.
fname = input('Enter file name : ')
num_words = 0
with open(fname,'r') as f1:
for line in f1:
words = line.split()
num_words += len(words)
print("Number of words : ",num_words)
Thus the program is written to find the word count from a text.