title: Open a file for input and output in Python
body: <p>I have the following code which is intended to remove specific lines of a file.  When I run it, it prints the two filenames that live in the directory, then deletes all information in them.  What am I doing wrong?  I'm using Python 3.2 under Windows.</p>
  
  <pre><code>import os
  
  files = [file for file in os.listdir() if file.split(".")[-1] == "txt"]
  
  for file in files:
      print(file)
      input = open(file,"r")
      output = open(file,"w")
  
      for line in input:
          print(line)
          # if line is good, write it to output
  
      input.close()
      output.close()
  </code></pre>