What is file.encoding convention?

Naoki INADA songofacandy at gmail.com
Thu Jul 23 05:06:56 CEST 2009

In document <http://docs.python.org/library/

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

But in logging.StreamHandler.emit() ::

                    if (isinstance(msg, unicode) and
                        getattr(stream, 'encoding', None)):
                        #fs = fs.decode(stream.encoding)
                            stream.write(fs % msg)
                        except UnicodeEncodeError:
                            #Printing to terminals sometimes fails.
For example,
                            #with an encoding of 'cp1251', the above
write will
                            #work if written to a stream opened or
wrapped by
                            #the codecs module, but fail when writing
to a
                            #terminal even when the codepage is set to
                            #An extra encoding step seems to be
                            stream.write((fs % msg).encode
                        stream.write(fs % msg)
                except UnicodeError:
                    stream.write(fs % msg.encode("UTF-8"))

And behavior of sys.stdout in Windows::
>>> import sys
>>> sys.stdout.encoding
>>> u = u"あいう"
>>> u
>>> print >>sys.stdout, u
>>> sys.stderr.write(u)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordinal not in range(128)

What is file.encoding convention?
If I want to write a unicode string to a file(-like) that have
encoding attribute, I should do
(1) try: file.write(unicode_str),
(2) except UnicodeEncodeError: file.write(unicode_str.encode
like logging?
It seems agly.

More information about the Python-list mailing list