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: ")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With