Request for Enhancement

Darrell Gallion darrell at dorb.com
Wed Aug 30 22:15:06 EDT 2000


fp=open(xxx)
 while 1:
     l=fp.readline()
     if not l:
             break
     print l

For speed, maybe something like so:
(untested)

class LineBuf:
    def __init__(self, fn, bufSz=0xfffff);
        self._fp=open(fn)
        self._buf=self._fp.read(bufSz)
        self._bufSz=bufSz
        self._offset=0
        self._eof=0

    def readline(self):
        start=self._offset
        end=string.find(self._buf, '\n', start)
        if end==-1:
            if self._eof:
                return self._buf[start:]
            newBuf=self._fp.read( self._bufSz)
            self._buf=self._buf[start:]+newBuf
            if len(newBuf) < self._bufSz:
                self._eof=1

        end=string.find(self._buf, '\n', start)
        self._offset=end
        return  self._buf[start:end]

Seems like someone must have written this same code many times already.
Maybe the library already buffers well enough and this isn't needed ?

--Darrell

----- Original Message -----
From: "Samuel A. Falvo II" <kc5tja at garnet.armored.net>
> I have need to process very large text files in Python, but I don't have
any
> idea how long the files are going to be in real-world situations.  It is
> unfortunate that there is no F.eof() function, where F is a Python file.
>
> Here's what I *want*:
>
> while not F.eof():
> l = F.readline()
> ...process line...
>






More information about the Python-list mailing list