by j_baker · 13y ago · 0 comments
And besides that, the author's solution is just good for one situation: when you want to read something line-by-line, which isn't always the case. For binary files, you may want to do something like this (untested code):
data = infile.read(256):
while data:
do_something(data)
data = infile.read(256)
Also, it seems like the author hasn't heard of enumerate: http://docs.python.org/2/library/functions.html#enumerateSome formats, such as csv do make sense to read in line-by-line though.
for one, variance in the line lengths (by byte count) will probably force the usage of inefficient buffer sizes
also, the parent comment gave one universal example: binary files
The context of the question and answers was 'a LinkedIn group for professional Python programmers'.
2. Test that it works
3. Optimize (if needed)
If your file is small enough and is a trivial percentage of the overall run time, who cares how it is read?