Skip to content

Instantly share code, notes, and snippets.

@ashildebrandt
Created December 3, 2014 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashildebrandt/29d117801e7185ba2451 to your computer and use it in GitHub Desktop.
Save ashildebrandt/29d117801e7185ba2451 to your computer and use it in GitHub Desktop.
Wordcount (determines the wordcount of a directory of .txt files)
"""
ash_wordcount.py
by me@aaronhildebrandt.com
Crawls through every .txt file in the current directory and gives you a
total word count for all the documents.
Prerequisities:
None
Usage:
python wordcount.py
"""
import os
file_list = os.listdir(".")
textfiles = []
wordcount = 0
for f in file_list:
if f[-3:] == "txt":
textfiles.append(f)
for f in textfiles:
file = open(f)
while True:
line = file.readline()
if not line:
break
wordcount += len(line.split(None))
print
print "Files:\t\t", len(textfiles)
print "Wordcount:\t", wordcount
print "Average:\t", (wordcount/len(textfiles)), "words/file"
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment