I am using Python 3 (recently switched from Python 2). My code usually works on Linux, but sometimes (not often) on Windows. According to the Python 3 documentation for open()
, the default encoding for a text file is from locale.getpreferredencoding()
, unless the encoding
argument is supplied. I want this default value to be utf-8
for my project, no matter what OS it is running on (currently it is always UTF-8 for Linux, but not for Windows). There are many open()
calls in the project, and I don't want to add encoding='utf-8'
to all of them. Thus, I want to change my preferred language encoding on Windows, as Python 3 sees it.
I found the previous question, β Changing theβ preferred language encoding β , in which the accepted answer was accepted, so I thought I was fine. But, unfortunately, none of the proposed commands in this answer and the first comment work for me on Windows. in particular, this accepted answer and its first comment suggest running chcp 65001
and set PYTHONIOENCODING=UTF-8
, and I tried set PYTHONIOENCODING=UTF-8
see the transcript below from my cmd window:
> py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit() > chcp 65001 Active code page: 65001 > py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit() > set PYTHONIOENCODING=UTF-8 > py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit()
Note that even after both of the proposed commands, my open file encoding is still cp1252
instead of the supposed utf-8
.
source share