If you pass it an open file it will keep it open (reading from the current position), if you pass a string then read_csv
will open and close the file.
In python if you open a file but forget to close it, python will close it for you at the end of the function block (during garbage collection).
def foo():
f = open("myfile.csv", "w")
...
f.close() # isn't actually needed
i.e. if you call a python function which opens a file, unless the file object is returned, the file is automagicallymatically closed.
Note: The preferred syntax is the with
block (which, as well as closing f
at the end of the with
block, defines the f
variable only inside the with
block):
def foo():
with open("myfile.csv", "w") as f:
...