This question has already been answered:
I can not get string data from the stdout output of the subprocess module:
import subprocess cmd = "ping ya.ru" response = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(response.stdout) At the output I get byte data.
b'\r\n\x8e\xa1\xac\xa5\xad \xaf\xa0\xaa\xa5\xe2\xa0\xac\xa8 \xe1 ya.ru [93.158.134.3] \xe1 32 \xa1\xa0\xa9\xe2\xa0\xac\xa8 \xa4\xa0\xad\xad\xeb\xe5:\r\n\x8e\xe2\xa2\xa5\xe2 \xae\xe2 93.158.134.3: \xe7\xa8\xe1\xab\xae \xa1\xa0\xa9\xe2=32 \xa2\xe0\xa5\xac\xef=3\xac\xe1 TTL=54\r\n\x8e\xe2\xa2\xa5\xe2 \xae\xe2 93.158.134.3: \xe7\xa8\xe1\xab\xae \xa1\xa0\xa9\xe2=32 \xa2\xe0\xa5\xac\xef=3\xac\xe1 TTL=54\r\n\x8e\xe2\xa2\xa5\xe2 \xae\xe2 93.158.134.3: \xe7\xa8\xe1\xab\xae \xa1\xa0\xa9\xe2=32 \xa2\xe0\xa5\xac\xef=2\xac\xe1 TTL=54\r\n\x8e\xe2\xa2\xa5\xe2 \xae\xe2 93.158.134.3: \xe7\xa8\xe1\xab\xae \xa1\xa0\xa9\xe2=32 \xa2\xe0\xa5\xac\xef=3\xac\xe1 TTL=54\r\n\r\n\x91\xe2\xa0\xe2\xa8\xe1\xe2\xa8\xaa\xa0 Ping \xa4\xab\xef 93.158.134.3:\r\n \x8f\xa0\xaa\xa5\xe2\xae\xa2: \xae\xe2\xaf\xe0\xa0\xa2\xab\xa5\xad\xae = 4, \xaf\xae\xab\xe3\xe7\xa5\xad\xae = 4, \xaf\xae\xe2\xa5\xe0\xef\xad\xae = 0\r\n (0% \xaf\xae\xe2\xa5\xe0\xec)\r\n\x8f\xe0\xa8\xa1\xab\xa8\xa7\xa8\xe2\xa5\xab\xec\xad\xae\xa5 \xa2\xe0\xa5\xac\xef \xaf\xe0\xa8\xa5\xac\xa0-\xaf\xa5\xe0\xa5\xa4\xa0\xe7\xa8 \xa2 \xac\xe1:\r\n \x8c\xa8\xad\xa8\xac\xa0\xab\xec\xad\xae\xa5 = 2\xac\xe1\xa5\xaa, \x8c\xa0\xaa\xe1\xa8\xac\xa0\xab\xec\xad\xae\xa5 = 3 \xac\xe1\xa5\xaa, \x91\xe0\xa5\xa4\xad\xa5\xa5 = 2 \xac\xe1\xa5\xaa\r\n' An attempt to decode using .decode ('utf-8') returns an error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8e in position 2: invalid start byte The best solution I have found right now is to decode ignoring errors and manually specify the ascii encoding. But I'm not sure that on other computers and with other commands, the code will give adequate output.
print(response.stdout.decode('ascii', 'ignore'))