python - Break or exit out of "with" statement? -
i'd exit out of with
statement under conditions:
with open(path) f: print 'before condition' if <condition>: break #syntax error! print 'after condition'
of course, above doesn't work. there way this? (i know can invert condition: if not <condition>: print 'after condition'
-- way above?)
the best way encapsulate in function , use return
:
def do_it(): open(path) f: print 'before condition' if <condition>: return print 'after condition'
Comments
Post a Comment