12 March 2004 30 comments Python
UPDATE 2 (November 2017)
Sorry for not having updated this in so many years. 2004 was a different Peter and I'm sorry if people landed on this blog post and got the wrong idea.
To read lines from a file do this:
with open('somefile.txt') as f: for line in f: print(line)
This works universally in Python 2 and Python 3. It reads one line at a time by iterating till it finds line breaks.
readlines(). The answer is in the name.
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 line at a time.
xreadlines() also reads one line at a time. I've changed the example code above.