Python 3.7+ cannot print unicode characters when output is redirected to file - is this a bug?

Thomas Passin list1 at tompassin.net
Sun Nov 13 10:45:57 EST 2022


On 11/13/2022 9:49 AM, Jessica Smith wrote:
> Consider the following code ran in Powershell or cmd.exe:
> 
> $ python -c "print('└')"
>> 
> $ python -c "print('└')" > test_file.txt
> Traceback (most recent call last):
>    File "<string>", line 1, in <module>
>    File "C:\Program Files\Python38\lib\encodings\cp1252.py", line 19, in encode
>      return codecs.charmap_encode(input,self.errors,encoding_table)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u2514' in
> position 0: character maps to <undefined>
> 
> Is this a known limitation of Windows + Unicode? I understand that
> using -x utf8 would fix this, or modifying various environment
> variables. But is this expected for a standard Python installation on
> Windows?
> 
> Jessica


This also fails with the same error:

$ python -c "print('└')" |clip


More information about the Python-list mailing list