What's the advantage of using with .. as in pyhon?

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2010-04-29T14:58:33Z Indexed on 2010/04/29 15:07 UTC
Read the original article Hit count: 254

Filed under:
|
with open("hello.txt", "wb") as f:
    f.write("Hello Python!\n")

seems to be the same as

f = open("hello.txt", "wb")
f.write("Hello Python!\n")
f.close()

What's the advantage of using open .. as instead of f = ? Is it just syntactic sugar? Just saving one line of code?

© Stack Overflow or respective owner

Related posts about python

Related posts about with-statement