Understanding stdin stdout stderr [duplicate]

孤街醉人 提交于 2019-11-27 21:16:29
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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!