[Tutor] Buffering in file.read()

Dave Kuhlman dkuhlman at rexx.com
Thu May 8 19:40:58 CEST 2008


On Thu, May 08, 2008 at 09:10:29PM +0530, Sandip Bhattacharya wrote:
> 
> Suppose I need to
> write a program which needs to parse lots of  binary files. This requires
> multiple reads of small amounts of data. Obviously if I need to do this
> very often, I need to do some kind of buffering to reduce IO.
> 
> My question is this - does Python have its own buffering while doing
> file.read()? In that case, I can stop worrying about writing my own
> buffering logic and just read like I desire.
> 

Take a look at the optional "bufsize" argument to the "open" built-in
function:

    "The optional bufsize argument specifies the file's desired buffer
    size: 0 means unbuffered, 1 means line buffered, any other positive
    value means use a buffer of (approximately) that size. A negative
    bufsize means to use the system default, which is usually line
    buffered for tty devices and fully buffered for other files. If
    omitted, the system default is used."

       -- http://docs.python.org/lib/built-in-funcs.html#l2h-54

Looks to me that the buffering is done for you, but that you can
control it a bit.

- Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list