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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asdsd sir

    #1

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

    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"|pyth on 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,somethi ng like....

    #someprog.py
    import sys
    print "hello"|sys.std in.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

  • Diez B. Roggisch

    #2
    Re: Some general questions about using "stdin&quo t;,"stdout "....

    asdsd sir wrote:
    [color=blue]
    > 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"|pyth on 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,somethi ng like....
    >
    > #someprog.py
    > import sys
    > print "hello"|sys.std in.read()
    >
    > .....the screen hangs..why is that?isn't the same situation as "cat"?[/color]

    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.std in.read()

    OTH is inside python, and | is not the pipe-operator, but the binary
    or-operator. Consider this:
    [color=blue][color=green][color=darkred]
    >>> print 1 | 2[/color][/color][/color]
    3

    But:[color=blue][color=green][color=darkred]
    >>> print "hello" | "some other string"[/color][/color][/color]
    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.
    [color=blue]
    > 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...[/color]

    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

    Comment

    Working...