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
Create a text file in a specific loaction of interes
On the same location as the text file, create a python program file.
n python Program, import sys and open a text file with argument "sys.argv[1]"
using read() and split(), split the lines in the file into a sequence of words
using len() count the number of words in the text file
In command prompt, initiate python followed by program name and text file name to get the output
#program for getting the word count from the contents of a file using command line argument
#Developed by: P.Sasinthar
#Register Number:23012532
import sys
with open(sys.argv[0],'r') as f:
num_words=0
for i in f:
word=i.split()
num_words+=len(word)
print("Number of words={}".format(num_words))
Thus the program is written to find the word count from the contents of a file using command line arguments.