Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37175 > unrolled thread

need explanation

Started bykwakukwatiah@gmail.com
First post2013-01-21 10:06 -0600
Last post2013-01-21 11:42 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  need explanation kwakukwatiah@gmail.com - 2013-01-21 10:06 -0600
    Re: need explanation Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-01-21 11:21 +0100
    Re: need explanation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-21 11:42 +0000

#37175 — need explanation

Fromkwakukwatiah@gmail.com
Date2013-01-21 10:06 -0600
Subjectneed explanation
Message-ID<mailman.735.1358762820.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [next] | [standalone]


#37178

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-01-21 11:21 +0100
Message-ID<0fhvs9-56h.ln1@satorlaser.homedns.org>
In reply to#37175
Am 21.01.2013 17:06, schrieb kwakukwatiah@gmail.com:
> please I need some explanation on sys.stdin  and sys.stdout, and piping out

http://www.catb.org/esr/faqs/smart-questions.html

Uli

[toc] | [prev] | [next] | [standalone]


#37179

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-01-21 11:42 +0000
Message-ID<50fd2995$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#37175
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web