title: for line in open(filename)
body: <p>I frequently see python code similar to </p>
  
  <pre><code>for line in open(filename):
      do_something(line)
  </code></pre>
  
  <p>When does filename get closed with this code?</p>
  
  <p>Would it be better to write</p>
  
  <pre><code>with open(filename) as f:
      for line in f.readlines():
          do_something(line)
  </code></pre>