1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Break or exit out of "with" statement?

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'd just like to exit out of a with statement under certain conditions:

    with open(path) as f:
    print 'before condition'
    if <condition>: break #syntax error!
    print 'after condition'


    Of course, the above doesn't work. Is there a way to do this? (I know that I can invert the condition: if not <condition>: print 'after condition' -- any way that is like above?)

    Continue reading...

Compartilhe esta Página