AndersonFirmino
3/16/2016 - 12:46 PM

word_count.py

import sys

def count_words(text_file):
    file = open(textfile,'r+')
    word_list = {}
    
    for word in file.read().split():
        if word not in word_list:
            word_list[word] = 1
        else:
            word_list[word] += 1
    return word_list

textfile = sys.argv[1]
list_of_words = count_words(textfile)

for word in sorted(list_of_words, key=list_of_words.get, reverse=True):
    print word, list_of_words[word]