In <[EMAIL PROTECTED]>, Michael B.
Trausch wrote:
> However, when I attempt to redirect the output to a file:
>
> [EMAIL PROTECTED]:~/tmp$ python test.py >f
> Traceback (most recent call last):
> File "test.py", line 6, in <module>
> print u"This is Unicode code point %d (0x%x): %s" % (x, x,
> unichr(x))
> UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in
> position 39: ordinal not in range(128)
>
> This is slightly confusing to me. The output goes all the way to the
> end of the program when it is not redirected. Why is Python treating
> the situation differently when the output is redirected?
If you print to a terminal `sys.stdout` is connected to that terminal and
there are ways to figure out that it is a terminal (`os.isatty()`) and
which encoding the terminal excepts. At least in most cases. But there
is no way to tell what encoding a file or pipe should have. So Python
refuses to guess.
If an encoding could be determined the `sys.stdout.encoding` attribute is
set to the name, otherwise it's `None`.
Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list