-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.py
More file actions
28 lines (23 loc) · 765 Bytes
/
Copy pathwordcount.py
File metadata and controls
28 lines (23 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""count the numbee of occurences of each word in a text file"""
import sys
def main():
"""count the words in a text file"""
if len(sys.argv) < 2 :
print('Usage: python wordcount <filename>')
print('where filename is name of text file')
else:
filename = sys.argv[1]
counters = {}
with open(filename, 'r') as f:
content= f.read()
words = content.split()
for word in words:
word = word.upper()
if word not in counters:
counters[word] = 1
else:
counters[word] += 1
for word, count in counters.items():
print(word, count)
if __name__ == '__main__':
main()