[Python-ideas] Python 3 open() text files: make encoding parameter optional for cross-platform scripts

Yuval Greenfield ubershmekel at gmail.com
Sat Jun 8 20:26:01 CEST 2013


On Sat, Jun 8, 2013 at 9:02 PM, Stephen J. Turnbull <stephen at xemacs.org>wrote:

> MRAB writes:
>
>  > 'open' defaults to universal newline support when opening for
>  > reading (though that's not possible when opening for writing!), and
>  > it would be nice if it also defaulted to a 'universal' encoding,
>  > i.e. UTF-8.
>
> There's no such thing as a universal encoding.  Unicode is a
> universal character set in the sense that it can encode all
> characters, but there is no universal encoding that can be used to
> decode all texts.
>
> If the OS's default encoding is not UTF-8, then you can and should bet
> that most files on that system will not be in UTF-8.  That's still
> true today.  Few users will be made happy by a Python that forces them
> to do something special to read files in the default encoding.
>
> The real question is why your system's default encoding is something
> that makes you unhappy, isn't it?
>
>
The real question is on which side of the following tradeoff you want to be.


Make python consistent and explicit across platforms when handling text
files. Do you want this to work:

    open('//remote_pc/text_file', 'w').write('whatever')

    # on remote_pc
    open('text_file').read()




Or have python implicitly play nice with the platform's native encoding,
i.e. have this work:

    open('a_file_i_wrote_in_this_platforms_notepad').read()
    open('a_file_i_want_to_open_in_this_platforms_notepad',
'w').write('whatever')


Both are legitimate decisions. Personally I favor the first because more
often than not files aren't encoded in the platform's chosen encoding, so
it's better to be explicit and consistent.



Yuval
utf-8-4-life
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130608/484ad2cd/attachment.html>


More information about the Python-ideas mailing list