What is file.encoding convention?

Naoki INADA songofacandy at gmail.com
Fri Jul 24 02:49:26 CEST 2009

>  What is the encoding of sys.stderr in your example?
Sorry, I missed. It is cp932

> So the problem is essentially this: if a stream has an encoding
> attribute, sometimes it is a wrapped stream which encodes Unicode
> (e.g. a stream obtained via the codecs module) and sometimes it is not
> (e.g. sys.stdout, sys.stderr).

Yes! I confused by it.

>> The encoding that this file uses. When Unicode strings are written to a file,
>>  they will be converted to byte strings using this encoding. In addition,
>> when the file is connected to a terminal, the attribute gives the encoding
>> that the terminal is likely to use

I feel this doc means "file object with encoding attribute encodes
regardless it is tty or not" and sys.stdout/stderr defies convention.

If this doc means "file object encodes unicode if it isn't tty.", I
should write
like below::

if not afile.isatty():
    if getattr(afile, "encoding") is not None:
elif getattr(afile, "encoding") is not None:
    afile.write(unicode_str.encode(fallback_encoding)) # utf8,
defaultencoding, preferedencoding, ...

"Writing unicode to a file(-like)" is a simple requirement.
Does python have any simple resolution for it?

More information about the Python-list mailing list