Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Some general questions about using "stdin","stdout"....

Reply
Thread Tools

Some general questions about using "stdin","stdout"....

 
 
asdsd sir
Guest
Posts: n/a
 
      02-16-2006
Hi!I'm new in Python and i'd like to ask some general questions about
stdin,stdout...

Firstly...

if we type like something like :
cat "file.txt"|python somefile.py

#somefile.py
import sys
text=sys.stdin.read()


....then "sys.stdin.read()" will read from "cat"s stdout...
However,if i type inside a program,something like....

#someprog.py
import sys
print "hello"|sys.stdin.read()

......the screen hangs..why is that?isn't the same situation as "cat"?

in addition to this...
Why can't i "write" to the stdin?
Isn't it being used as a file object like all the others?
for example
sys.stdin.close() or
open('sys.stdin','w+') or
sys.stdin.write("something") etc... don't work...

At last,let's consider "sys.stdin.read(3)"
If i type that,the program "waits" for me to type some characters,and then
prints the first three...
However,doesn't this action actually include a "writing" to stdin and then
a "reading" from that?

I'm really confused...
Any help would be highly appreciated...
Thanks a lot.

__________________________________________________ _______________
Free blogging with MSN Spaces http://spaces.msn.com/?mkt=nl-be

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-16-2006
asdsd sir wrote:

> Hi!I'm new in Python and i'd like to ask some general questions about
> stdin,stdout...
>
> Firstly...
>
> if we type like something like :
> cat "file.txt"|python somefile.py
>
> #somefile.py
> import sys
> text=sys.stdin.read()
>
>
> ...then "sys.stdin.read()" will read from "cat"s stdout...
> However,if i type inside a program,something like....
>
> #someprog.py
> import sys
> print "hello"|sys.stdin.read()
>
> .....the screen hangs..why is that?isn't the same situation as "cat"?


Obviously not... The stdin is a read-only file that you can read from the
data that is passed via a pipe to your application. You did tha piping by
using

cat "file.txt" | python somefile.py

That establihes the pipe between cat's stdout(!) and python's stdin. That's
the reason that | is called "pipe" or "pipe-operator" in the SHELL(!)

print "hello"|sys.stdin.read()

OTH is inside python, and | is not the pipe-operator, but the binary
or-operator. Consider this:

>>> print 1 | 2

3

But:
>>> print "hello" | "some other string"

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'str' and 'str'


So you don't write something to stdin by that. Instead it waits endlessly,
trying to read something that is piped to it from the outside. But if that
was the case, it would puke on you with the above error message.

> in addition to this...
> Why can't i "write" to the stdin?
> Isn't it being used as a file object like all the others?
> for example
> sys.stdin.close() or
> open('sys.stdin','w+') or
> sys.stdin.write("something") etc... don't work...


Because it is defined that way. I suggest you read up on unix piping to
grasp the concepts behind it - python only follows these.


Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Some Body At Microsoft Please Do Some Thing About My Imposter In XP And Vista General? Windows 64bit 3 08-05-2009 05:19 AM
General....very general.... no important for u forever hi Python 0 03-18-2009 08:21 AM
Some General Questions about Ruby Vinh Chuc Ruby 7 04-05-2007 02:29 PM
Some general questions about C and good practice KimmoA C Programming 66 09-14-2006 08:06 PM
Some general DVD observations and questions Jake DVD Video 17 10-15-2004 03:01 AM



Advertisments