In Python, it is recommended to use with
when opening files, e.g.:
with open('x.json') as f:
data = json.load(f)
But is it okay to shorten it as follows?
data = json.load(open('x.json'))
The with
statement makes sure that the file is closed, but would Python close the file in my second example as well, as there's no pointer to the file and it's clear that the file won't be accessed anymore?