Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Program to get the input from command line and perform word count opertation in python


import sys
from subprocess import check_output
num_words = 0
print ("Enter the file Name")
fopen=open(sys.argv[1], 'r')
with  fopen as f:
    for line in f:
        words = line.split()
        num_words += len(words)
print("Number of words:")
fopen.close()
print(num_words)
cmd=sys.argv[2]+" "+sys.argv[1]
result=check_output(cmd, shell=True).decode()
print ("The content of the file is")
print (result)

No comments:

Post a Comment

Flag Counter