how to check how many bytes are available to read() ?

Grant Edwards grante at visi.com
Thu Mar 14 23:43:35 EST 2002


In article <2b57f654.0203142026.68ec3fc7 at posting.google.com>, wealthychef wrote:

> I have a file object f.  I want to know if there are any bytes
> to read from it.

A worthy and not infrequent desire.

> I have tried using select.select([], [f], []),

That will return when f is writable.  In other words write(x)
will not block.  At least not for some values of x.

If you do select.select([f],[],[]), then it will return return
when f.read() will not block.  Note: calling read() on a
regular file never blocks, therefore select() returns
immediately for regular files.

> but I have found that select will return [],[f],[] even if
> there is nothing to read in f, so that when I call
> f.readline(), it blocks.

Call select.select([f],[],[])

> Is there a function or some way i can peer into f and see if
> it has any bytes for me?
>
> f is actually someprocess.fromchild, by the way.

If f is a pipe, then you can use select -- at least under Unix.
I don't know about Windows.

-- 
Grant Edwards                   grante             Yow!  .. Like I always
                                  at               say -- nothing can beat
                               visi.com            the BRATWURST here in
                                                   DUSSELDORF!!



More information about the Python-list mailing list