I know
with open(my_file, 'r') as f:
pass
opens and closes the file. Also,
f = open(my_file, 'r'); f.close()
does the same.
What about this:
open(my_file, 'r')
My actual question case is this:
import json;
json.load(open(my_file, 'r'))
vs
import json;
with open(my_file, 'r') as f:
j = json.load(f)
I guess that technically, the file should stay open, but I'm pretty sure that since the file object was not assigned, it was closed right away, by Python's garbage collector.
- Am I right?
- Is this a good practice?
- Any difference between Python versions here?