title: Python - Finding word frequencies of list of words in text file
body: <p>I am trying to speed up my project to count word frequencies. I have 360+ text files, and I need to get the total number of words and the number of times each word from another list of words appears. I know how to do this with a single text file. </p>
  
  <pre><code>&gt;&gt;&gt; import nltk
  &gt;&gt;&gt; import os
  &gt;&gt;&gt; os.chdir("C:\Users\Cameron\Desktop\PDF-to-txt")
  &gt;&gt;&gt; filename="1976.03.txt"
  &gt;&gt;&gt; textfile=open(filename,"r")
  &gt;&gt;&gt; inputString=textfile.read()
  &gt;&gt;&gt; word_list=re.split('\s+',file(filename).read().lower())
  &gt;&gt;&gt; print 'Words in text:', len(word_list)
  #spits out number of words in the textfile
  &gt;&gt;&gt; word_list.count('inflation')
  #spits out number of times 'inflation' occurs in the textfile
  &gt;&gt;&gt;word_list.count('jobs')
  &gt;&gt;&gt;word_list.count('output')
  </code></pre>
  
  <p>Its too tedious to get the frequencies of 'inflation', 'jobs', 'output' individual. Can I put these words into a list and find the frequency of all the words in the list at the same time? Basically <a href="https://stackoverflow.com/questions/4520876/counting-the-frequency-of-specific-words-in-text-file">this</a> with Python.</p>
  
  <p>Example: Instead of this:</p>
  
  <pre><code>&gt;&gt;&gt; word_list.count('inflation')
  3
  &gt;&gt;&gt; word_list.count('jobs')
  5
  &gt;&gt;&gt; word_list.count('output')
  1
  </code></pre>
  
  <p>I want to do this (I know this isn't real code, this is what I'm asking for help on):</p>
  
  <pre><code>&gt;&gt;&gt; list1='inflation', 'jobs', 'output'
  &gt;&gt;&gt;word_list.count(list1)
  'inflation', 'jobs', 'output'
  3, 5, 1
  </code></pre>
  
  <p>My list of words is going to have 10-20 terms, so I need to be able to just point Python toward a list of words to get the counts of. It would also be nice if the output was able to be copy+paste into an excel spreadsheet with the words as columns and frequencies as rows</p>
  
  <p>Example:</p>
  
  <pre><code>inflation, jobs, output
  3, 5, 1
  </code></pre>
  
  <p>And finally, can anyone help automate this for all of the textfiles? I figure I just point Python toward the folder and it can do the above word counting from the new list for each of the 360+ text files. Seems easy enough, but I'm a bit stuck. Any help? </p>
  
  <p>An output like this would be fantastic:
      Filename1
      inflation, jobs, output
      3, 5, 1</p>
  
  <pre><code>Filename2
  inflation, jobs, output
  7, 2, 4
  
  Filename3
  inflation, jobs, output
  9, 3, 5
  </code></pre>
  
  <p>Thanks!</p>