[EMAIL PROTECTED] wrote:

> if it is a medium size to large file, then it is recommended to use
> xreadlines instead of readlines.
> here the example from Python help:
>
> import xreadlines, sys
>
> for line in xreadlines.xreadlines(sys.stdin):
>     pass

In recent versions of Python, xreadlines() has become a method of file
objects, so it's unnecessary to import xreadlines:

myfile = open('data.txt', 'r')
for line in myfile.xreadlines():
    process(line)

For the O.P.:  The difference between readlines() and xreadlines(), is
that readlines() will read the entire file in a single chunk -- if
it's an 11MB file, then you've just filled an 11MB block of memory
(plus a bit more for overhead).  But xreadlines() reads in smaller
chunks -- theoretically a single line at a time, but I believe that
it's probably actually buffered in 1024-byte blocks (or something like
that).  Both methods, when used in a loop as above, will produce each
line of the file successively.  The only difference is that
readlines() will (potentially) use up more memory, while xreadlines()
has a tiny bit more overhead (and, ISTM, more potential for
concurrency issues).

Jeff Shannon
Technician/Programmer
Credit International


_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython

Reply via email to