Looping through a file a block of text at a time not by line

Fredrik Lundh fredrik at pythonware.com
Wed Jun 14 04:04:31 EDT 2006


Rune Strand wrote:

> Probably a more terse way to do this, but this seems to work
> import os
>
> offset = 0
> grab_size = 6000
> file_size = os.stat('hotels.xml')[6]

ouch.  why not just loop until f.read returns an empty string ?

> f = open('hotels.xml', 'r')
>
> while offset < file_size:
>    f.seek(offset)
>    data_block = f.read(grab_size)
>    offset += grab_size
>    print data_block
> f.close()

here's a shorter and more reliable version:

    f = open(filename)
    for block in iter(lambda: f.read(6000), ""):
        ... process block

here's the terse version:

    for block in iter(lambda f=open(filename): f.read(6000), ""): ...

:::

what happens if a <Rate> element straddles the border between two 6000
byte blocks, btw ?

</F> 






More information about the Python-list mailing list