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

Guido van Rossum guido at python.org
Sun Sep 10 19:04:56 CEST 2006


On 9/10/06, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Another aspect of the textfile discussion.
> sys.stdin and sys.stdout are for now, concretely, byte streams (AFAIK,
> at least under Unix).

No, they are conceptually text streams, because that's what they are
on Windows, which the only remaining platform where you can currently
experience the difference between text and byte streams.

> Yet it must be possible to read/write text to and
> from them.

I'd turn it around. If you want to read bytes from stdin (sometimes a
useful thing for filters), in Py3k you better dig out the underlying
byte stream and use that.

> 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) ?

You've got it backwards.

> 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() ?

Not in my view of the future. :-)

> Also, consider a "script.py" beginning with:
>
> import sys, text
> if len(sys.argv) > 1:
>     f = textfile(sys.argv[1], "r")
> else:
>     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
> ?

All sorts of things are different when reading stdin vs. opening a
filename. e.g. stdin may be a pipe.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list