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
Import sys module to use command line arguments.
Use the open() key with argument sys.argv[1] which means the first index of given argument
Iterate the content of the file using for loop.
Split the contents into each line using .split() function
Iterate the list of lines and increment the value of variable (word) each time
Give the argument "python prgm.py EX12.txt" on the command line.
End of the Program.
#Program for getting the word count from the contents of a file using command line arguments.
#Developed by: Nithishkumar P
#Register number: 212221230070
import sys
fp=open(sys.argv[1])
count=0
for data in fp:
l=data.split()
for i in l:
count+=1
print("No. of words in the file:",count)
Thus the program is written to find the word count from the contents of a file using command line arguments.