Count the word frequencies within a given directory
#!/usr/bin/python3
import os
import re
from os.path import join
from collections import Counter, OrderedDict

def count_words(directory, ext): # don't use the name dir, it's a builtin function
    """Counts word frequencies in a directory of files.

    Keyword arguments:
    directory -- count_words will search this directory recursively
    ext -- the extension of files that you wish to count

    Returns an OrderedDict, from most to least frequent.

    (You don't strictly need a docstring, but it's a good habit to get into.
    See PEP 257.)"""

    # Initialize the counter
    word_counter = Counter()

    # Personally I like to break my code into small, simple functions
    # This code could be inline in the loop below,
    # but I think it's a bit clearer this way.
    def update_counter(word_counter, filename):
        '''updates word_counter with all the words in open(filename)'''
        with open(filename, 'r') as f:
            try:
                # Two things:
                # First, I'm updating counter here,
                # instead of creating an enormous string.
                # Each file string only exists long enough for me to search it.

                # Second and less important,
                # I chose to lower the full text first, to make the regex simpler.
                word_counter.update(re.findall('[a-z_]+', f.read().lower()))
            except UnicodeDecodeError:
                # In python3 if your file isn't UTF-8
                # then read() throws an exception.
                # Maybe you want this behavior,
                # but I'd rather just warn and continue.
                print("Warning: couldn't decode", filename)

    # Using os.walk instead of glob
    for root, dirs, files in os.walk(directory):
        for fname in files:
            if fname.endswith(ext):
                update_counter(word_counter, join(root, fname))

    # words_counter.most_common() does exactly the sort you are looking for
    return OrderedDict(word_counter.most_common())