Most common words in a text file of about 1.1 million words I'm learning programming with Python.

I’ve written the code below for finding the most common words in a text file that has about 1.1 million words. It is working fine, but I believe there is always room for improvement.

When run, the function in the script gets a text file from the command-line argument sys.argv[1], opens the file in read mode, converts the text to lowercase, makes a list of words from the text after removing any whitespaces or empty strings, and stores the list elements as dictionary keys and values in a collections.Counter object. Finally, it returns a dictionary of the most common words and their counts. The words.most_common() method gets its argument from the optional top parameter.

import sys
import collections

def find_most_common_words(textfile, top=10):    
    """ Return the most common words in a text file. """

    textfile = open(textfile)
    text = textfile.read().lower()
    textfile.close()
    words = collections.Counter(text.split()) # how often each word appears

    return dict(words.most_common(top))

filename = sys.argv[1]
top_five_words = find_most_common_words(filename, 5)
This is actually quite good. Good use of the collections module. 


One improvement I can think of is switching to the with open(...) as: structure, that way the file will automatically be closed when finished processing.
Secondly it is standard in python to use a if __name__ == '__main__': block.
Lastly sys.argv will work ok for programs with not many arguments, but I'd recommend looking at the argparse module.