If I do (in Python):
text = open("filename").read()
is the file automatically closed?
The garbage collector would be activated at some point, but you cannot be certain of when unless you force it.
The best way to ensure that the file is closed when you go out of scope just do this:
with open("filename") as f: text = f.read()
also one-liner but safer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With