Created
December 3, 2014 19:38
-
-
Save ashildebrandt/29d117801e7185ba2451 to your computer and use it in GitHub Desktop.
Wordcount (determines the wordcount of a directory of .txt files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 "Files:\t\t", len(textfiles) | |
print "Wordcount:\t", wordcount | |
print "Average:\t", (wordcount/len(textfiles)), "words/file" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment