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

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -