The following program is very simple. It launches a subprocess which runs a Windows port of the Unix utility less.
import subprocess
subprocess.run('less.exe', input='Macarrão é uma delícia.', encoding='utf-8')
The input is:
Macarrão é uma delícia.
The output, though, comes out as:
Macarrão é uma delícia.
What is the explanation for this? I have noticed that running chcp 65001
before running my python code fixes the problem, but looking through a related post I'm not sure it's the best way to go about it. Quoting from the accepted answer:
chcp 65001 is very dangerous. Unless a program was specially designed to work around defects in the Windows’ API (or uses a C runtime library which has these workarounds), it would not work reliably. Win8 fixes ½ of these problems with
cp65001
, but the rest is still applicable to Win10.
I'm running Python 3.7.0 on Windows 10 64-bit.