I am new to Python and feel confused about when I should call close.
For instance, consider this example from the csv documentation.
import csv
with open('some.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row
There is no reader.close() in the end of this task.
Q> How do I find out whether or not a function call need to be followed by a close call?
Functions that return resources that need to be cleaned up will have documentation that explains that.
csv.readerdoesn’t open a file, or otherwise create a closable resource. Theopencall does, but thewithstatement takes care of closing it for you.