Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using readlines in Python bad code?

Tags:

python

I got downvoted for an answer using file.readlines. The critic said that using readlines is crap code (along with other very rude statements).

Is it so bad?

like image 229
Paulo Scardine Avatar asked Dec 26 '10 12:12

Paulo Scardine


2 Answers

I assume that the problem was the fact that readlines() loads the whole file into memory, which - theoretically - can be a lot.

A lazy approach (iterating over the file and reading progressively as needed) is indeed better in terms of memory usage. Not sure about efficiency, though.

like image 93
Kos Avatar answered Sep 27 '22 23:09

Kos


No, readlines is fine. You just have to remember, that the whole file is stored in memory at one point.

like image 37
miku Avatar answered Sep 27 '22 23:09

miku