Website hosting service by

 Back to Index

5.17 xreadlines -- Efficient iteration over a file

New in version 2.1.


Deprecated since release 2.3. Use for line in file instead.

This module defines a new object type which can efficiently iterate over the lines of a file. An xreadlines object is a sequence type which implements simple in-order indexing beginning at 0, as required by for statement or the filter() function.

Thus, the code


import xreadlines, sys

for line in xreadlines.xreadlines(sys.stdin):

has approximately the same speed and memory consumption as


while 1:
    lines = sys.stdin.readlines(8*1024)
    if not lines: break
    for line in lines:

except the clarity of the for statement is retained in the former case.


xreadlines( fileobj)
Return a new xreadlines object which will iterate over the contents of fileobj. fileobj must have a readlines() method that supports the sizehint parameter. Note: Because the readlines() method buffers data, this effectively ignores the effects of setting the file object as unbuffered.

An xreadlines object s supports the following sequence operation:

Operation  Result 
s[i] i'th line of s

If successive values of i are not sequential starting from 0, this code will raise RuntimeError.

After the last line of the file is read, this code will raise an IndexError.




2002-2004 Webhosting Service


Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to


Domain registration : Buy domain name or register domain name from $5.95/year only


Cheap domain registration : Register domain name or buy domain name, including free domain hosting services offers cheap domain registration, domain name transfer and domain search services