Last active
July 12, 2016 20:32
-
-
Save jeffehobbs/f8b66bd26261f9e87d5209b70bdfc6ea to your computer and use it in GitHub Desktop.
average word/line/character count for text files in directory
This file contains hidden or 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
| #!/usr/bin/env python | |
| import sys, os | |
| DIRECTORY = sys.argv[1] | |
| ARTICLES = 0 | |
| LINECOUNT = [] | |
| WORDCOUNT = [] | |
| CHARACTERCOUNT = [] | |
| CONTRAIN_SAMPLE_SIZE = 1000 | |
| for subdir, dirs, files in os.walk(DIRECTORY): | |
| for file in files: | |
| filepath = subdir + os.sep + file | |
| if ((filepath.endswith(".txt")) and (ARTICLES < CONTRAIN_SAMPLE_SIZE)): | |
| #print filepath | |
| num_lines = 0 | |
| num_words = 0 | |
| num_chars = 0 | |
| try: | |
| with open(filepath, 'r') as f: | |
| for line in f: | |
| words = line.split() | |
| num_lines += 1 | |
| num_words += len(words) | |
| num_chars += len(line) | |
| except: | |
| print("ERROR OPENING " + file) | |
| ARTICLES = ARTICLES + 1 | |
| LINECOUNT.append(num_lines) | |
| WORDCOUNT.append(num_words) | |
| CHARACTERCOUNT.append(num_chars) | |
| #print(num_words) | |
| average_words = sum(WORDCOUNT) / float(len(WORDCOUNT)) | |
| average_lines = sum(LINECOUNT) / float(len(LINECOUNT)) | |
| average_characters = sum(CHARACTERCOUNT) / float(len(CHARACTERCOUNT)) | |
| print "ARTICLES ANALYZED : " + str(ARTICLES) | |
| print "AVERAGE WORD COUNT: " + str(int(average_words)) | |
| print "AVERAGE LINE COUNT: " + str(int(average_lines)) | |
| print "AVERAGE CHAR COUNT: " + str(int(average_characters)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment