I have a similar issue to How to get the output of subprocess.check_output() python module? but the solution there does not work for German Windows.
I execute the following script in python 3.10 in wsl2 / ubuntu:
import subprocess
import sys
ipconfig = subprocess.check_output(["ipconfig.exe", "/all"]).decode(sys.stdout.encoding)
This leads to
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in position 90: invalid start byte
The bytes returned from check_output are:
b'\r\nWindows-IP-Konfiguration\r\n\r\n Hostname . . . . . . . . . . . . : XXXXXXXXXXXX\r\n Prim\x84res DNS-Suffix .
and sys.stdout.encoding is utf-8. But this is wrong, as \x84 is invalid under utf-8.
The \x84 here is an ä. According to https://tripleee.github.io/8bit/#84 this corresponds to e.g. cp850 for western european encoding, which makes sense.
How can I get the correct encoding programmatically?
locale.getpreferredencoding()give you? Hopefully that should give you the normal encoding for your Windows install (as opposed tosys.stdout.encoding, which is specifically what Python uses when writing to stdout).cp1252which is wrong, too, as\x84is„notäforcp1252.LookupError: unknown encoding: oemwhen called from WSL (calling from windows works, but the script needs to run in WSL)ipconfig = subprocess.check_output(["ipconfig.exe", "/all"], encoding="cp850")cp850.