examples/dictionary/count_words.py
from __future__ import print_function
import sys

filename = 'README'
if len(sys.argv) > 1:
    filename = sys.argv[1]
print(filename)

count = {}

with open(filename) as fh:
    for full_line in fh:
        line = full_line.rstrip('\n')
        for word in line.split(' '):
            if word not in count:
                count[word] = 0

            count[word] += 1
 
for word in count:
    print("'{}' {}".format(word, count[word]))