This code
with open(myfile) as f:
data = f.read()
process(data)
equivalent to this
try:
f = open(myfile)
data = f.read()
process(f)
finally:
f.close()
or next?
f = open(myfile)
try:
data = f.read()
process(f)
finally:
f.close()
This article: http://effbot.org/zone/python-with-statement.htm suggests (if I understand it correctly) that the latter is true. However, the first would make sense to me. If I am wrong, what am I missing?
source
share