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
Open the file in read mode and handle it in test mood.
Read the text using read() function.
Split the text using space separator.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
#Developed by: S.VENGADA KRISHNAN
#Register Number: 212223110061
import sys
fp=open(sys.argv[1])
wordcount=0
for i in fp:
words=i.split()
wordcount+=len(words)
print("Total no of words in file is",wordcount)
fp.close()
Thus the program is written to find the word count from the contents of a file using command line arguments.