To readline() or readlines()

http://gnosis.cx/publish/programming/charming_python_5.txt

12th 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:

 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()

The 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 .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.



Comment

21 comments so far Show all 21 comments
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.