KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I can't figure out how to handle exception for python 'with' statement. If I have a code: with open("a.txt") as f: print f.readlines() I really want to handle 'file not found exception' in order to do something. But I can't write with open("a.txt") as f: print f.readlines() except: print 'oops' and can't write with open("a.txt") as f: print f.readlines() else: print 'oops' Enclosing with in a try/except statement doesn't work either, and an exception is not raised. What can I do in order to process failure inside with statement in a Pythonic way?
Tags (comma-separated)
Save Edits
Cancel