title: writing the console output to a file in python
body: <p>I want the console output to be written into a file,
  how do you redirect the console outut to
  a file 'c1.txt'?
  plz help.
  This is my code:</p>
  
  <pre><code>from collections import Counter
  from glob import iglob
  import re
  import os
  
  def removegarbage(text):
      # Replace one or more non-word (non-alphanumeric) chars with a space
      text = re.sub(r'\W+', ' ', text)
      text = text.lower()
      return text
  
  topwords = 100
  folderpath = 'path/to/directory'
  counter = Counter()
  for filepath in iglob(os.path.join(folderpath, '*.txt')):
      with open(filepath, 'r') as filehandle:
          counter.update( removegarbage(filehandle.read()).split() )
  
  for word, count in counter.most_common(topwords):
      print ('{}: {}'.format(count, word))
  </code></pre>