Help optimize a script?

Paul Winkler slinkp23 at yahoo.com
Wed Oct 17 14:59:23 EDT 2001


On Wed, 17 Oct 2001 10:51:51 -0700, Joseph Santaniello
<someone at _no-spam_arbitrary.org> wrote:

(snip)
># while is used cuz line in readlines() used too much ram with
># huge files.
(snip)

Try xreadlines - either the xreadlines module (new in 2.1),
or the file.xreadlines method (also new in 2.1).

If you can't use python 2.1 for some reason, you can get the same effect
"by hand" using the sizehint parameter to readlines.

while 1:
    # read lines up to approx. 8 KB at a time
    lines = sys.stdin.readlines(8*1024) 
    if not lines: break
    for line in lines:
        do_something_with_line()

HTH,

-- Paul Winkler






More information about the Python-list mailing list