FAQ
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

Search Discussions

  • Dan at Feb 16, 2006 at 3:16 pm
    Hello.

    If you're new to Python, then input/output isn't the best place to
    start. Begin with the tutorial:
    http://docs.python.org/tut/tut.html
    Other documentation is also linked to from there.

    However, I will briefly answer your questions.
    print "hello"|sys.stdin.read()
    In Python the | operator has a different meaning than in a shell. In
    Python it means "bitwise or":
    print 5 | 9
    13

    Why can't i "write" to the stdin?
    There may be some contorted way to do that under Unix, but it's not a
    good idea. You can pipe input to your program from a shell (as you've
    already done), but it's not a good idea to redirect input from within
    your program.

    You can accomplish the same thing like this:

    if i_want_to_redirect_input:
    my_input_source = open('file.txt')
    else:
    my_input_source = sys.stdin
    # Now read from my_input_source instead of stdin.

    --
    They had a big meeting, drank some beer and had some pizza and
    decided 'A' would be 65.
    - Jim Greenly, professor at Georgia Institute of Technology
  • Diez B. Roggisch at Feb 16, 2006 at 4:07 pm

    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


    From http Thu Feb 16 17:17:23 2006
    From: http (Paul Rubin)
    Date: 16 Feb 2006 08:17:23 -0800
    Subject: pythonic way of 'b = list(a); b.append(4)"
    References: <1140105883.069632.262530@g43g2000cwa.googlegroups.com>
    Message-ID: <7xhd6zjn1o.fsf@ruckus.brouhaha.com>

    "szabi" <szabolcs.berecz at gmail.com> writes:
    I have a list of three values and want to call a function with four
    parameters. I would like
    to write something like:

    a = [1, 2, 3]
    f(*a, 4)
    f(*a+[4]) seems to work.
  • Asdsd sir at Feb 16, 2006 at 11:06 pm
    thank you very much for your help...
    my big mistake,was to believe that "|" is the pipe symbol for both,unix and
    python...
    it is really annoying,how such a simple thing can mess things

    Thank you for clearing this out.

    _________________________________________________________________
    Free blogging with MSN Spaces http://spaces.msn.com/?mkt=nl-be
  • Steve Holden at Feb 17, 2006 at 4:36 am

    asdsd sir wrote:
    thank you very much for your help...
    my big mistake,was to believe that "|" is the pipe symbol for both,unix and
    python...
    it is really annoying,how such a simple thing can mess things

    Thank you for clearing this out.
    It is indeed annoying when assumptions we carry from one environment
    turn out to be untrue for another. I can almost hear the unspoken
    thought ("But Perl is similar to shell scripting in this way, so why
    aren't other languages?") - though I may be doing you a disservice
    assuming that you know anything about Perl.

    You will find as your programming experience increases that the
    different languages you learn are appropriate for different purposes,
    and have different advantages and disadvantages. Python excels at
    expressing algorithms in an unambiguous and easily-readable way. It's
    worth taking the time to learn it, and you are clearly already on your way.

    I look forward to reading your next question. Welcome to comp.lang.python.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 16, '06 at 2:14p
activeFeb 17, '06 at 4:36a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase