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(\"Hell
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.
f.write
f.close()
with
f
with.