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')) 

Reported as a duplicate by jfs , insolor , Cheg , andreymal , br3t participants on Sep 16 '17 at 10:27 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    The characters in the Windows command line are encoded with the cp866 encoding table. Decode using it:

     print(response.stdout.decode('cp866')) 

    By the way, your example can be improved by synchronously outputting each line from the process. That is, you do not wait for the process to complete completely, and as you receive data from the output stream, print them to the console:

     import subprocess process = subprocess.Popen(["ping", "ya.ru"], stdout=subprocess.PIPE) # синхронный busy spin, работа с потоком вывода, пока процесс не отработает while True: output = process.stdout.readline() if output == b'' and process.poll() is not None: break if output: print(output.strip().decode('cp866')) # когда процесс завершится, poll() вернет код завершения, а не None rc = process.poll() 
    • When using .decode ('cp866'), the program displays an empty string. - Alpensin
    • characters can be encoded using cp866, but not required (for example, python probably uses cp1251 on Russian Windows when redirecting). For details, see my answer to the duplicate question - jfs