data = [json.loads(line) for line in open('music.json', 'r')]
In this way, do I need to close the file somehow? It doesn't have a handle to close it.
Is it safe?
The standard/idiomatic way to do something like this is to wrap it inside a with
block. This would make your line look something like this:
with open('music.json', 'r') as music:
data = [json.loads(line) for line in music]
The with
block takes a closable object (a file in this case) and a variable. It assigns the variable to the object for the duration of the block of code, and when the block finishes, it automatically closes the object. For more information, see https://docs.python.org/3/reference/compound_stmts.html#the-with-statement.