Skip to content

Commit 1d627fd

Browse files
committed
adding word count function file
1 parent 9d29262 commit 1d627fd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

word_count.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/local/bin/python3
2+
3+
import os
4+
import sys
5+
import pprint
6+
7+
def everyWordCount(filename):
8+
"""
9+
This function takes filename as one of the arguments from
10+
the command line and prints count of each word in that
11+
file.
12+
"""
13+
# count of every word is stored in the dictionary
14+
word_dict = {}
15+
f = open(filename, "r")
16+
for line in f:
17+
word_list = line.split()
18+
for word in word_list:
19+
if word in word_dict.keys():
20+
word_dict[word] += 1
21+
else:
22+
word_dict[word] = 1
23+
return word_dict
24+
25+
26+
# filename is given from command line as the first arugument
27+
filename = sys.argv[1]
28+
pprint.pprint(everyWordCount(filename))

0 commit comments

Comments
 (0)