Examples in "13.1. csv — CSV File Reading and Writing" don't close files

Hi, I have a suggestion for the csv module's documentation. The examples in http://docs.python.org/library/csv.html show file reading like this: reader = csv.reader(open("some.csv", "rb")) This leaves the file handle open after use, without a way to close it. A better example might be ifile = open("some.csv", "rb") reader = csv.reader(ifile) * ... process file ...* ifile.close() Thanks for all your good work, Neil

Hi Neil, thanks for your email. On Tue, Feb 8, 2011 at 11:01, Neil Vass <neil.vass@gmail.com> wrote:
Hi, I have a suggestion for the csv module's documentation. The examples in http://docs.python.org/library/csv.html show file reading like this: reader = csv.reader(open("some.csv", "rb")) This leaves the file handle open after use, without a way to close it. A better example might be ifile = open("some.csv", "rb") reader = csv.reader(ifile) ... process file ... ifile.close()
I can see your point, and f.e. in Python3 I'd rather use the with statement to open the file, but I fear that reformatting the examples as you propose will make them too big and they'll loose their compactness and easy-to-get-to-the-point. Maybe a warning at the beginning of the examples section that they are just \examples\ and so file handling is not done in a state-of-the-art manner? What other readers of docs@ think? Cheers, -- Sandro Tosi (aka morph, morpheus, matrixhasu) My website: http://matrixhasu.altervista.org/ Me at Debian: http://wiki.debian.org/SandroTosi
participants (2)
-
Neil Vass
-
Sandro Tosi