Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > need explanation

Reply
Thread Tools

need explanation

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-21-2013
Am 21.01.2013 17:06, schrieb http://www.velocityreviews.com/forums/(E-Mail Removed):
> please I need some explanation on sys.stdin and sys.stdout, and piping out


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

Uli

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-21-2013
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
 
Reply With Quote
 
 
 
 
kwakukwatiah@gmail.com
Guest
Posts: n/a
 
      01-21-2013
please I need some explanation on sys.stdin and sys.stdout, and piping out
 
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
Re: What is a GCHandle? (Need simple explanation ;o) darrel ASP .Net 0 10-20-2004 04:34 PM
The following photo is all the explanation you need: MCSE 10 07-21-2004 05:05 PM
The following photo is all the explanation you need: MCSD 6 07-21-2004 05:05 PM
New to .net need help/explanation Dave ASP .Net 4 07-01-2004 04:00 PM
Need Explanation Kaladhaur Palaniappa Perl 0 08-07-2003 09:47 AM



Advertisments