need explanation

Discussion in 'Python' started by Ulrich Eckhardt, Jan 21, 2013.

  1. Ulrich Eckhardt, Jan 21, 2013
    #1
    1. Advertising

  2. On Mon, 21 Jan 2013 10:06:41 -0600, kwakukwatiah wrote:

    > please I need some explanation on sys.stdin and sys.stdout, and piping
    > out


    "stdin" and "stdout" (and also stderr) are three special, standard,
    system files used by console programs that read and write text. That's
    nearly all of them.

    "stdin" is short for "standard input". Likewise for "standard output" and
    "standard error".

    When you give the Python command:

    print "Hello World"

    the string "Hello World" is written to stdout, which then displays it in
    the console.

    "stderr" is similar, except that it is used for error messages. And stdin
    is used for input, rather than output.

    So, in Python, I can do this:

    py> import sys
    py> sys.stdout.write("Hello world\n")
    Hello world

    But of course normally you would just use print.

    Using sys.stdout, sys.stdin and sys.stderr in Python is usually
    considered moderately advanced. Beginners do not usually need to care
    about them.


    These three special files do *not* live on the disk. There is no disk
    file called "stdout" unless you create one yourself, and if you do, it
    won't be special, it will just be an ordinary file with the name "stdout".

    These standard files are used in Unix and Linux, and less so in Windows,
    for console applications. For example, under Linux I might write this
    command:

    [steve@ando ~]$ touch foo
    [steve@ando ~]$ ls foo
    foo

    The output of the `ls` command is written to stdout, which displays it on
    the console. But I can *redirect* that output to a real file on disk:

    [steve@ando ~]$ ls foo > /tmp/a
    [steve@ando ~]$ cat /tmp/a
    foo


    Errors don't go to stdout, they go to stderr:

    [steve@ando ~]$ ls bar > /tmp/a
    ls: bar: No such file or directory


    Because there is no file called "bar", the `ls` command writes an error
    message to stderr. Even though I am redirecting stdout, I am not touching
    stderr, so it prints to the console.

    Of course there is a way to redirect stderr as well:


    [steve@ando ~]$ ls bar 2> /tmp/a
    [steve@ando ~]$ cat /tmp/a
    ls: bar: No such file or directory


    Similarly, you can redirect stdin, or you can use a pipe | to turn the
    output of one command into the input of another command. This is mostly
    useful when using something like command.com in Windows, not so common in
    Python.


    --
    Steven
    Steven D'Aprano, Jan 21, 2013
    #2
    1. Advertising

  3. Ulrich Eckhardt

    Guest

    please I need some explanation on sys.stdin and sys.stdout, and piping out
    , Jan 21, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaladhaur Palaniappa

    Need Explanation

    Kaladhaur Palaniappa, Aug 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    994
    Kaladhaur Palaniappa
    Aug 7, 2003
  2. Dave
    Replies:
    4
    Views:
    385
  3. darrel
    Replies:
    0
    Views:
    396
    darrel
    Oct 20, 2004
  4. 0k-
    Replies:
    4
    Views:
    262
    Steven D'Aprano
    Jan 18, 2007
  5. Need some explanation

    , Jul 8, 2005, in forum: C Programming
    Replies:
    70
    Views:
    1,309
    Mark McIntyre
    Jul 22, 2005
Loading...

Share This Page