how to calculate the size of sys.stdin?

Grant Edwards grante at visi.com
Wed Apr 11 22:04:01 EDT 2001


In article <mailman.987030856.375.python-list at python.org>, Robin Thomas wrote:

>sys.stdin is more like a pipe, and not a regular file. You
>can't ask a pipe how many bytes it contains before you read
>from it.

Actually, you can.  

At least on some Unix systems, the FIONREAD ioctl() call will
tell you how many bytes there are in a pipe waiting to be read.
IIRC, it also works on some tty devices as well. Probably not
very portable, and somebody could have shoved more bytes into
the other end after the ioctl() and before the read(), so you
could get more that you expect, but you shouldn't get less.

My gut feeling is that if you think you need to know how many
bytes there are in a pipe, you're program should probably be
structured differently.

-- 
Grant Edwards                   grante             Yow!  With YOU, I can be
                                  at               MYSELF... We don't NEED
                               visi.com            Dan Rather...



More information about the Python-list mailing list