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 toinput()
); -
stdout
is used for the output ofprint()
and expression statements and for the prompts ofinput()
; - The interpreter’s own prompts and its error messages go to
stderr
.
These streams are regular text files like those returned by the open()
function. Their parameters are chosen as follows:
-
The character encoding is platform-dependent. Under Windows, if the stream is interactive (that is, if its
isatty()
method returnsTrue
), the console codepage is used, otherwise the ANSI code page. Under other platforms, the locale encoding is used (seelocale.getpreferredencoding()
).Under all platforms though, you can override this value by setting the
PYTHONIOENCODING
environment variable before starting Python. - When interactive, standard streams are line-buffered. Otherwise, they are block-buffered like regular text files. You can override this value with the
-u
command-line option.
Note
To write or read binary data from/to the standard streams, use the underlying binary buffer
object. For example, to write bytes to stdout
, use sys.stdout.buffer.write(b'abc')
.
However, if you are writing a library (and do not control in which context its code will be executed), be aware that the standard streams may be replaced with file-like objects like io.StringIO
which do not support the buffer
attribute.
Please login to continue.