To readline() or readlines()
http://gnosis.cx/publish/programming/charming_python_5.txt12th of March 2004
When you create a file object in Python you can read from it in several different ways. Not until today did I understand the difference between readline() and readlines(). The answer is in the name. readline() reads one line character at a time, readlines() reads in the whole file at once and splits it by line.
These would then be equivalent:
for line in f.readlines():
print line
f.close()
# ...and...
f = open('somefile.txt','r')
for line in f.read().split('\n'):
print line
f.close()
The xreadlines() function should be used for big files:
for line in f.xreadlines():
print line
f.close()
From Charming Python
" The difference between .readline() and .readlines() is that
the latter, like .read(), reads in an entire file at once.
.readlines() automatically parses the read contents into a
list of lines, thereby enabling the for ... in ... construct
common in Python. Using .readline() reads in just a single
line from a file at a time, and is generally much slower than
.readlines(). Really the only reason to use the
.readline() version is if you expect to read very large
files that might exceed available memory."
UPDATE
Thanks commentors for pointing out that I and the Charmed Python book got it completely wrong. readline() reads one character at a time. xreadlines() reads one line at a time. I've changed the example code above.
Tweet