To slurp a file I can do either:
with open('foo', 'r') as fd:
content = fd.read()
or
content = open('foo').read()
Is there any advantages to use the with
statement here?
To slurp a file I can do either:
with open('foo', 'r') as fd:
content = fd.read()
or
content = open('foo').read()
Is there any advantages to use the with
statement here?
The first method ensures that the file will be closed no matter what. It's like doing:
try:
fd = open('foo')
content = fd.read()
# ... do stuff here
finally:
fd.close()