count items in generator

BartlebyScrivener rpdooling at gmail.com
Sun May 14 01:05:47 EDT 2006


Still new. I am trying to make a simple word count script.

I found this in the great Python Cookbook, which allows me to process
every word in a file. But how do I use it to count the items generated?

def words_of_file(thefilepath, line_to_words=str.split):
    the_file = open(thefilepath)
    for line in the_file:
        for word in line_to_words(line):
                yield word
    the_file.close()
for word in words_of_file(thefilepath):
    dosomethingwith(word)

The best I could come up with:

def words_of_file(thefilepath, line_to_words=str.split):
    the_file = open(thefilepath)
    for line in the_file:
        for word in line_to_words(line):
                yield word
    the_file.close()
    len(list(words_of_file(thefilepath)))

But that seems clunky.




More information about the Python-list mailing list