BartlebyScrivener <[EMAIL PROTECTED]> wrote: > 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.
My preference would be (with the original definition for words_of_the_file) to code numwords = sum(1 for w in words_of_the_file(thefilepath)) Alex -- http://mail.python.org/mailman/listinfo/python-list