#!/usr/bin/env python """ count the words in a file """ # The name of the file filename = 'moby_dick.txt' # dictionary of word frequencies count = {} # open the file for reading input = open(filename, 'r') # loop over 30 lines for i in range(30): line = input.readline() words = line.split() for word in words: count[word] = count.get(word,0) + 1 for word in count: print word, ' : ', count[word]