In: Computer Science
Python
Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Code:
import collections
import string
import sys
#function to count no of times a letter appeared
def count(textFile):
#opens text file as f
with open(textFile, 'r') as f:
#read content of the file
content = f.read()
#all letters of alphabet
letter = string.ascii_letters
#count no of times all letter in text file appears with function
collections.Counter()
counts = collections.Counter(i for i in content if i in
letter)
#return output
return counts
#calling function count with text file name taken from command
line
#sys.argv[1] is used to take command line arguement
print(count(sys.argv[1]))
Code Photo:
Text File:
Output: