Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, loop through files in a folder and do a word count

Tags:

python

I am new to python and I need to write a script that counts all the words in all the txt files in a directory. This is what I have so far, the else works when just opening a txt file, but when I enter a directory it fails. I know I need an append somewhere, I have tried it a few different ways but with little luck.

*edit I would like the results to be lumped together. So far its 2 separate results. I tried making a new list and having it appended with counter. but it broke. Thanks again, this is a good community

import re
import os
import sys
import os.path
import fnmatch
import collections

def search( file ):

    if os.path.isdir(path) == True:
        for root, dirs, files in os.walk(path):
            for file in files:
                words = re.findall('\w+', open(file).read().lower())
                ignore = ['the','a','if','in','it','of','or','on','and','to']
                counter=collections.Counter(x for x in words if x not in ignore)
                print(counter.most_common(10))

    else:
        words = re.findall('\w+', open(path).read().lower())
        ignore = ['the','a','if','in','it','of','or','on','and','to']
        counter=collections.Counter(x for x in words if x not in ignore)
        print(counter.most_common(10))

path = input("Enter file and path, place ' before and after the file path: ")
search(path)

raw_input("Press enter to close: ")
like image 308
Garrett Avatar asked Aug 21 '26 14:08

Garrett


1 Answers

Change line 14 to:

words = re.findall('\w+', open(os.path.join(root, file)).read().lower())

Also, if you replace the input line with

path = raw_input("Enter file and path")

Then you won't need to include ' before and after the path

like image 130
David Robinson Avatar answered Aug 24 '26 05:08

David Robinson