title: Catching an exception while using a Python 'with' statement
body: <p>I can't figure out how to handle exception for python 'with' statement. If I have a code:</p>
  <pre><code>with open(&quot;a.txt&quot;) as f:
      print f.readlines()
  </code></pre>
  <p>I really want to handle 'file not found exception' in order to do somehing. But I can't write</p>
  <pre><code>with open(&quot;a.txt&quot;) as f:
      print f.readlines()
  except:
      print 'oops'
  </code></pre>
  <p>and can't write</p>
  <pre><code>with open(&quot;a.txt&quot;) as f:
      print f.readlines()
  else:
      print 'oops'
  </code></pre>
  <p>enclosing <code>with</code> in a try/except statement doesn't work neither, and an exception is not raised. What can I do in order to process failure inside <code>with</code> statement in a Pythonic way?</p>