title: In Python, how can I open a file and read it on one line, and still be able to close the file afterwards?
body: <p>While working through this exercise I ran into a problem.</p>
  
  <pre><code>from sys import argv
  from os.path import exists
  
  script, from_file, to_file = argv
  print "Copying from %s to %s" % (from_file, to_file)
  
  # we could do these two on one line too, how?
  input = open(from_file)
  indata = input.read()
  
  print "The input file is %d bytes long" % len(indata)
  print "Does the output file exist? %r" % exists(to_file)
  print "Ready, hit RETURN to continue, CTRL-C to abort."
  
  raw_input()
  
  output = open(to_file, 'w')
  output.write(indata)
  print "Alright, all done."
  output.close()
  input.close()
  </code></pre>
  
  <p>The line <code># we could do these two on one line too, how?</code> is what’s confusing me. The only answer I could come up with was:</p>
  
  <pre><code>indata = open(from_file).read()
  </code></pre>
  
  <p>This performed the way I wanted to, but it requires me to remove:</p>
  
  <pre><code>input.close()
  </code></pre>
  
  <p>as the input variable no longer exists. How then, can I perform this close operation?</p>
  
  <p>How would you solve this?</p>