Python program to count the frequency of words in a given file
kw.py
from collections import Counter char=0 lines=0 for line in open(r'/home/kodingwindow/Weekdays'): char=char+len(line) lines+=1 print("Characters -",char) print("Lines -",lines) with open(r'/home/kodingwindow/Weekdays') as f: words=[word for line in f for word in line.split()] print("Word Count -",len(words)) c=Counter(words) for word,count in c.most_common(): print(word,"-",count)
Output
cat Weekdays Monday Monday Sunday Wednesday Monday Monday Sunday Wednesday Friday kodingwindow@kw:~$ python3 kw.py Characters - 69 Lines - 9 Word Count - 9 Monday - 4 Sunday - 2 Wednesday - 2 Friday - 1
Comments and Reactions
What Next?
Python Networking
Advertisement