[perl-python] 20050121 file reading & writing

Fredrik Lundh fredrik at pythonware.com
Sun Jan 23 18:56:59 EST 2005


Erik Max Francis wrote:

>> To do this efficiently on a large file (dozens or hundreds of megs), you should use the 
>> 'sizehint' parameter so as not to use too much memory:
>>
>> sizehint = 0
>> mylist = f.readlines(sizehint)
>
> It doesn't make any difference.  .readlines reads the entire file into memory at once.

except when it doesn't:

    readlines([sizehint])

    Read until EOF using readline() and return a list containing the
    lines thus read. If the optional sizehint argument is present, instead
    of reading up to EOF, whole lines totalling approximately sizehint
    bytes (possibly after rounding up to an internal buffer size) are read.
    Objects implementing a file-like interface may choose to ignore
    sizehint if it cannot be implemented, or cannot be implemented
    efficiently.

>>> f = open("ot.xml")
>>> s = f.readlines(1000)
>>> len(s)
157

>>> f = open("ot.xml")
>>> s = f.readlines()
>>> len(s)
48560

</F> 






More information about the Python-list mailing list