title: Using python "with" statement with try-except block
body: <p>Is this the right way to use the python "with" statement in combination with a try-except block?:</p>
  
  <pre><code>try:
      with open("file", "r") as f:
          line = f.readline()
  except IOError:
      &lt;whatever&gt;
  </code></pre>
  
  <p>If it is, then considering the old way of doing things:</p>
  
  <pre><code>try:
      f = open("file", "r")
      line = f.readline()
  except IOError:
      &lt;whatever&gt;
  finally:
      f.close()
  </code></pre>
  
  <p>Is the primary benefit of the "with" statement here that we can get rid of three lines of code?  It doesn't seem that compelling to me <strong>for this use case</strong> (though I understand that the "with" statement has other uses).</p>
  
  <p>EDIT: Is the functionality of the above two blocks of code identical?</p>
  
  <p>EDIT2: The first few answers talk generally about the benefits of using "with", but those seem of marginal benefit here.  We've all been (or should have been) explicitly calling f.close() for years.  I suppose one benefit is that sloppy coders will benefit from using "with".</p>