how to calculate the size of sys.stdin? 
Author Message
 how to calculate the size of sys.stdin?

Greetings,

Is there a way to calculate the size (in bytes) of sys.stdin?  I tried
to use the "stat" module to accomplish this, but os.stat() seems to
only accept the name of a file.  (i.e, the following doesn't work):

import os,stat
size = os.stat(sys.stdin.read())[stat.ST_SIZE]

Regards,
Graham

_____________________________________________________________________________
http://www.*-*-*.com/ - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Mon, 29 Sep 2003 06:33:50 GMT  
 how to calculate the size of sys.stdin?
Sez Graham Guttocks:

Quote:
> Is there a way to calculate the size (in bytes) of sys.stdin?

No, sys.stdin has no size.  What you can do is read from sys.stdin and check
how much you got:

input = sys.stdin.read()
size = len(input)

Peace,
  Kalle
--

Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]



Mon, 29 Sep 2003 06:47:20 GMT  
 how to calculate the size of sys.stdin?

| Is there a way to calculate the size (in bytes) of sys.stdin?  I tried
| to use the "stat" module to accomplish this, but os.stat() seems to
| only accept the name of a file.  (i.e, the following doesn't work):
|
| import os,stat
| size = os.stat(sys.stdin.read())[stat.ST_SIZE]

os.fstat(sys.stdin.fileno())

This makes sense only when stdin happens to be a disk file, of course.

I don't understand why you would try sys.stdin.read() in the above
example, so I don't know if this helps or not, but you'll know if
you try it.




Mon, 29 Sep 2003 07:37:50 GMT  
 how to calculate the size of sys.stdin?

Quote:

> import sys
> bytes = sys.stdin.read()
> size_of_stdin = len(bytes)

Perfect.  Thanks again folks!

Cheers,
Graham

_____________________________________________________________________________
http://movies.yahoo.com.au - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Mon, 29 Sep 2003 07:49:59 GMT  
 how to calculate the size of sys.stdin?

Quote:
----- Original Message -----

Subject: how to calculate the size of sys.stdin?

> Greetings,

> Is there a way to calculate the size (in bytes) of sys.stdin?  I tried
> to use the "stat" module to accomplish this, but os.stat() seems to
> only accept the name of a file.  (i.e, the following doesn't work):

> import os,stat
> size = os.stat(sys.stdin.read())[stat.ST_SIZE]

What you want is os.fstat():

    size = os.fstat(sys.stdin.fileno())[stat.ST_SIZE]

but the result is likely to be meaningless if sys.stdin is
a terminal, pipe, socket, etc.



Mon, 29 Sep 2003 07:10:46 GMT  
 how to calculate the size of sys.stdin?

Quote:
>Greetings,

>Is there a way to calculate the size (in bytes) of sys.stdin?  I tried
>to use the "stat" module to accomplish this, but os.stat() seems to
>only accept the name of a file.  (i.e, the following doesn't work):

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. Instead, you can
yourself count how many bytes you read from the pipe before there are no
more to read:

import sys
bytes = sys.stdin.read()
size_of_stdin = len(bytes)

Note that sys.stdin.read() reads as many bytes as possible until the "end"
of the pipe. read() will "block", or wait indefinitely, until sys.stdin
says, "no more bytes left, because the process writing to me closed me".

This way, if sys.stdin is actually being produced by another process's
stdout, sys.stdin.read() will block until the other process finishes
writing to it. Try this:

# save as out.py
import sys, time
num = 0
while num < 5:
     sys.stdout.write('a')
     time.sleep(1)
     num = num + 1

# save as in.py
import sys
bytes = sys.stdin.read()
print bytes

Then do this in the shell:

bash$ python out.py | python in.py

And you'll notice that in.py reads 5 bytes from sys.stdin, waiting
patiently for out.py to finish writing and close its end of the pipe.

Also try this:

bash$ python
Hi it's Python
 >>> import sys
 >>> bytes = sys.stdin.read()

And now Python will block, eating everything you type into the terminal,
until you indicate that you're done writing to the stdin pipe by typing
Ctrl-D. (Ctrl-D is a terminal control telling the terminal to close the
pipe; Ctrl-D present in a regular file does *not* mean end-of-file.)

Then try to read again:

 >>> sys.stdin.read()
''

It returns immediately! You didn't even get a chance to type anything,
because sys.stdin thinks, "I'm at the end of the pipe, so there's nothing
more to read".

Then do:

 >>> sys.stdin.seek(0)

which says, "rewind to the beginning of the file". Then:

 >>> sys.stdin.read()

and Python blocks again, waiting for you to type Ctrl-D to finish. Lots of fun.

--
Robin Thomas
Engineering
StarMedia Network, Inc.



Mon, 29 Sep 2003 07:15:02 GMT  
 how to calculate the size of sys.stdin?
Sez Chris Gonnerman:

Quote:
> ----- Original Message -----

> Subject: Re: how to calculate the size of sys.stdin?

> > Sez Graham Guttocks:
> > > Is there a way to calculate the size (in bytes) of sys.stdin?

> > No, sys.stdin has no size.  What you can do is read from sys.stdin and
> check
> > how much you got:

> sys.stdin has size if it is connected to a normal file, or
> to some device nodes (in unixoid OS environments).  Size is
> not available for terminals, pipes, etc.

Whoops!  Of course.  <whack, whack, whack>

Peace,
  Kalle
--

Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]



Mon, 29 Sep 2003 07:46:04 GMT  
 how to calculate the size of sys.stdin?

Quote:
----- Original Message -----

Subject: Re: how to calculate the size of sys.stdin?

> Sez Graham Guttocks:
> > Is there a way to calculate the size (in bytes) of sys.stdin?

> No, sys.stdin has no size.  What you can do is read from sys.stdin and
check
> how much you got:

sys.stdin has size if it is connected to a normal file, or
to some device nodes (in unixoid OS environments).  Size is
not available for terminals, pipes, etc.

I just tested with Python 1.5.2 under Linux 2.2 and got 0
for size of a terminal, but correct figures for file sizes
using redirection:

    $ python sizer.py
    0

    $ python sizer.py <sizer.py
    95

  sizer.py
< 1K Download


Mon, 29 Sep 2003 07:14:25 GMT  
 how to calculate the size of sys.stdin?

Quote:

>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...



Mon, 29 Sep 2003 10:04:01 GMT  
 how to calculate the size of sys.stdin?

Quote:


> >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.

That's interesting for a pet project of mine.  How on earth does one
go about learning things like that?  Is this sort of stuff in Stevens'
APUE?

Cheers,
M.

--
  We did requirements and task analysis, iterative design, and user
  testing. You'd almost think programming languages were an interface
  between people and computers.                    -- Steven Pemberton
          (one of the designers of Python's direct ancestor ABC)



Mon, 29 Sep 2003 16:19:55 GMT  
 how to calculate the size of sys.stdin?

Quote:



>> >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.

>That's interesting for a pet project of mine.  How on earth does one
>go about learning things like that?  

I learned it by reading Usenet and looking at device driver
source code.

Quote:
>Is this sort of stuff in Stevens' APUE?

Might be.  I don't have a copy at hand.

--
Grant Edwards                   grante             Yow!  He is the
                                  at               MELBA-BEING... the ANGEL
                               visi.com            CAKE... XEROX him... XEROX
                                                   him --



Tue, 30 Sep 2003 00:01:33 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. Chosing between sys.argv and sys.stdin

3. ADIR() Size vs Calculated Size

4. non-blocking sys.stdin.read() on win32?

5. banging head against sys.stdin and tkinter on win32

6. sys.stdin.readlines() in 2.2a ?

7. sys.stdin.read() HELP!

8. reusing sys.stdin?

9. Error using sys.stdin.read()

10. can i make sys.stdin.read() not wait?

11. sys.stdin.readline

12. Is there something magic about sys.stdin?

 

 
Powered by phpBB® Forum Software