22
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?

prosseek
  • 182,215
  • 215
  • 566
  • 871

3 Answers3

34

In order to be equivalent to the with statement version, the code you wrote should look instead like this:

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

While this might seem like syntactic sugar, it ensures that you release resources. Generally the world is more complex than these contrived examples and if you forget a try.. except... or fail to handle an extreme case, you have resource leaks on your hands.

The with statement saves you from those leaks, making it easier to write clean code. For a complete explanation, look at PEP 343, it has plenty of examples.

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
mg.
  • 7,822
  • 1
  • 26
  • 30
13

If f.write throws an exception, f.close() is called when you use with and not called in the second case. Also f has a smaller scope and the code is cleaner when using with.

Kathy Van Stone
  • 25,531
  • 3
  • 32
  • 40
3

The former still closes f if an exception occurs during the f.write().

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
djc
  • 11,603
  • 5
  • 41
  • 54