[Python-3000] sys.stdin and sys.stdout with textfile

Antoine Pitrou solipsis at pitrou.net
Sun Sep 10 16:27:12 CEST 2006


Another aspect of the textfile discussion.
sys.stdin and sys.stdout are for now, concretely, byte streams (AFAIK,
at least under Unix). Yet it must be possible to read/write text to and
from them.

So two questions:
 - Is there a builtin text.stdin / text.stdout counterpart to
sys.stdin / sys.stdout (the former being text versions, the latter raw
bytes versions) ?
Or a way to write: my_input_file = textfile(sys.stdin) ?
 - How is handled the default encoding ?
Does Python mandate setting an encoding before calling print() or
raw_input() ?

Also, consider a "script.py" beginning with:

import sys, text
if len(sys.argv) > 1:
    f = textfile(sys.argv[1], "r")
    f = text.stdin

Should encoding policy be chosen differently depending on whether the
script is called with:
    python script.py in.txt
or with:
    python script.py < in.txt



More information about the Python-3000 mailing list