I'm having a hard time using the subprocess.run
function with a command that contains accentuated characters (like "é" for example).
Consider this simple example :
# -*- coding: utf-8 -*-
import subprocess
cmd = "echo é"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
print("Output of subprocess.run : {}".format(result.stdout.hex()))
print("é char encoded manually : {}".format("é".encode("utf-8").hex()))
It gives the following output :
Output of subprocess.run : 820d0a
é char encoded manually : c3a9
I don't understand the value returned by subprocess.run
, shouldn't it also be c3a9
? I understand the 0d0a
is CR+LF, but why 82
?
Because of this, when I try to run this line :
output = result.stdout.decode("utf-8")
I get a UnicodeDecodeError Exception with the following message : 'utf-8' codec can't decode byte 0x82 in position 0: invalid start byte
I tried explicitly specifying the encoding format like this :
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, encoding="utf-8")
But this raises the same exception ('utf-8' codec can't decode byte 0x82 in position 0: invalid start byte
) when subprocess.run
is called.
I'm running this on Windows 10 with Python3.8.5.
I hope someone can help me with this, any hint ?