0

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?

5
  • What does locale.getpreferredencoding() give you? Hopefully that should give you the normal encoding for your Windows install (as opposed to sys.stdout.encoding, which is specifically what Python uses when writing to stdout). Commented Apr 13, 2023 at 8:35
  • Sadly, also UTF-8 in python on WSL. Python on windows produces cp1252 which is wrong, too, as \x84 is not ä for cp1252. Commented Apr 13, 2023 at 8:46
  • LookupError: unknown encoding: oem when called from WSL (calling from windows works, but the script needs to run in WSL) Commented Apr 13, 2023 at 9:00
  • ipconfig = subprocess.check_output(["ipconfig.exe", "/all"], encoding="cp850") Commented Apr 13, 2023 at 19:10
  • That works for my computer, but I want a solution how to get the correct encoding programatically, and not hardcode cp850. Commented Apr 14, 2023 at 8:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.