19

I'm trying to understand stdin stdout and stderr.

I see them used in people's code all the time and I can't understand exactly what they are. I am assuming that they have something to do with input/output but have been searching for an explanation online and can't find one. Does anybody know of a good link with an explanation or if it is simple enough to explain it would be a great help to me.

Since I am learning Python 3, examples in that would be helpful.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Alex Mollberg
  • 231
  • 3
  • 5
  • 9
  • These are related to `shell` not python. – Ashwini Chaudhary Jan 28 '13 at 04:29
  • Reading [this article](https://en.wikipedia.org/wiki/Standard_streams) is a great start. Feel free to ask any questions after that. – loopbackbee Jan 28 '13 at 04:32
  • 4
    @AshwiniChaudhary Not shell, *terminal*. – kojiro Jan 28 '13 at 04:34
  • I edited your question and removed the Python parts as this is not something specific to Python; but then I realized someone already answered the question in Python so I added a line to your question. Still doesn't need to be tagged as Python. – Burhan Khalid Jan 28 '13 at 04:44
  • @kojiro terminal is just an application which runs shell, in the end it's the shell which interacts with kernel. – Ashwini Chaudhary Jan 28 '13 at 04:46
  • @AshwiniChaudhary The shell doesn't provide the standard streams, and needn't be involved in the interaction between an application and those streams. – kojiro Jan 28 '13 at 05:34

2 Answers2

7
sys.stdin
sys.stdout
sys.stderr

File objects used by the interpreter for standard input, output and errors:

  • stdin is used for all interactive input (including calls to input());

  • stdout is used for the output of print() and expression statements and for the prompts of input();

  • The interpreter’s own prompts and its error messages go to stderr.

For your more understanding:

>>> import sys
>>> for i in (sys.stdin, sys.stdout, sys.stderr):
...     print i
... 
<open file '<stdin>', mode 'r' at 0x103451150>
<open file '<stdout>', mode 'w' at 0x1034511e0>
<open file '<stderr>', mode 'w' at 0x103451270>

mode r means reading and mode w means writing

Suku
  • 3,820
  • 1
  • 21
  • 23
3

Does this explain it well enough?

sys.stdin
sys.stdout
sys.stderr
File objects corresponding to the interpreter’s standard input, output and error streams.

stdin is used for all interpreter input except for scripts but including calls to input() and raw_input().

stdout is used for the output of print and expression statements and for the prompts of input() and raw_input().

The interpreter’s own prompts and (almost all of) its error messages go to stderr.
stdout and stderr needn’t be built-in file objects: any object is acceptable as long as it has a write() method that takes a string argument.
(Changing these objects doesn’t affect the standard I/O streams of processes executed by os.popen(), os.system() or the exec*() family of functions in the os module.)

To clairify

if I tell the python shell to do a print:

print 'what is your name?'

"what is your name" will go to stdout, whatever that is. If you haven't made any redirects that's by default the terminal you're using. You can interact with the standard streams in various different ways, for example:

sys.stdout.flush()

Tells the python shell to force any buffered information that I've printed to go to stdout right away.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • ok so input on the command line is assigned to sys.stdout in the program itself? – Alex Mollberg Jan 28 '13 at 04:34
  • 1
    @AlexMollberg - Not exactly. Input you give on the command line is given to the running programs' `stdin` stream. Things you print go to `stdout`... for example the line `print "hello"` doesn't tell the program where to print it to, it just goes to `stdout` which if you haven'tmade any redirects is the terminal (displayed on the monitor) – Mike Jan 28 '13 at 04:38
  • I'm confuse: `any buffered information that I've printed to go to stdout right away` but printed informations goes in `stdout` by defaullt, right? If so, what the point of `sys.stdout.flush()`? – JinSnow Mar 26 '17 at 12:28
  • printed information goes to `stdout` by default, but only when it's ready to go. Print operations are time expensive and thus aren't done instantly, but the information is buffered instead and then it prints when the system determines it should. This could be during a lull in the system or when there is "enough" in the buffer to go. The way to *force* it out is with a `flush` operation. – Mike Sep 19 '17 at 13:12