To write a python program for getting the word count from the contents of a file using command line arguments.
PC Anaconda - Python 3.7
Get the file name as input from the user.
Use for loop to count the number of words in the file.
Use split() to read the splitted words.We assume that words in a sentance are separted by a space character.
The length of the split list should equal the numbers of words in the test file.
You can refine the count by cleaning the string prior to splitting or validating the words after splitting.
End the program
fname=input("enter the file name")
num_words=0
with open(fname,'r') as f:
for line in f:
words=line.split()
num_words+=len(words)
print("Number of words: ",num_words)
Thus the program is written to find the word count from the contents of a file using command line arguments.