Advertisement
ashground

wordcount.py

Nov 1st, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. """
  2.  
  3.        wordcount.py
  4.        by aaron@andcuriouser.com
  5.      
  6.        Crawls through every .txt file in the current directory and gives you a
  7.        total word count for all the documents.
  8.      
  9.        Prerequisities:
  10.                None
  11.      
  12.        Usage:
  13.                python wordcount.py
  14.  
  15. """
  16.  
  17.  
  18. import os
  19.  
  20. file_list = os.listdir(".")
  21. textfiles = []
  22. wordcount = 0
  23.  
  24. for f in file_list:
  25.     if f[-3:] == "txt":
  26.         textfiles.append(f)
  27. for f in textfiles:
  28.     file = open(f)
  29.     while True:
  30.         line = file.readline()
  31.         if not line:
  32.             break
  33.         wordcount += len(line.split(None))
  34. print
  35. print "Files:\t\t", len(textfiles)
  36. print "Wordcount:\t", wordcount
  37. print "Average:\t", (wordcount/len(textfiles)), "words/file"
  38. print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement