title: Python, loop through files in a folder and do a word count
body: <p>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.</p>
  
  <p>*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</p>
  
  <pre><code>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: ")
  </code></pre>