Neither method is ideal because you want to ensure the file is closed at the end. For this purpose you can use with
to ensure the file is closed once you're finished processing it. This avoids mistakes where you might forget to call .close()
on the file. More info on context managers can be found here. Examples of this can be seen in the documentation.
So, something like:
with open('my_data.csv') as infile:
reader = csv.reader(infile)
data = list(reader)
Note, however, this loads the whole contents of the file into memory. As the documentation examples show, you could instead iterate through the reader
object if you don't need the whole file in memory.