I've seen that the best practice for working with files in Python is to use the with
block:
with open('file', 'r') as fi:
text = fi.read()
with open('file', 'w') as fi:
fi.write(text)
This way the files are automatically closed after you're done with them. But I get lazy, and in quick one-shot scripts I tend to do this instead:
text = open('file', 'r').read()
open('file', 'w').write(text)
Now obviously if I'm writing Real Softwareâ„¢ I should use the former, but I'd like to know what consequences the latter has (if any)?