12 March 2004 28 comments Python
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
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:
f = open('somefile.txt','r') 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()
xreadlines() function should be used for big files:
f = open('HUGE.log','r'): for line in f.xreadlines(): print line f.close()
From Charming Python
" The difference between
.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."
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.