4

Когда я использую:

subprocess.check_output('dir', shell=True).decode('utf-8')

то получаю эту ошибку:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 1: invalid start byte

Вот вывод print(subprocess.check_output('dir', shell=True)):

b' \x92\xae\xac \xa2 \xe3\xe1\xe2\xe0\xae\xa9\xe1\xe2\xa2\xa5 C \xad\xa5 \xa8\xac\xa5\xa5\xe2 \xac\xa5\xe2\xaa\xa8.\r\n \x91\xa5\xe0\xa8\xa9\xad\xeb\xa9 \xad\xae\xac\xa5\xe0 \xe2\xae\xac\xa0: 3858-A3CB\r\n\r\n \x91\xae\xa4\xa5\xe0\xa6\xa8\xac\xae\xa5 \xaf\xa0\xaf\xaa\xa8 C:\\Users\\User\\Desktop\\soft_and_code\\ph-dl\\kivy\\shareit\\filesharerkivy\\filesharekivy v1.3\\project\\optimazer 1\r\n\r\n27.08.2020  15:27    <DIR>          .\r\n27.08.2020  15:27    <DIR>          ..\r\n27.08.2020  15:27             1\xff157 cursor_info.py\r\n27.08.2020  10:41           713\xff820 output.mkv\r\n27.08.2020  10:42           715\xff370 output.mp4\r\n26.08.2020  19:54            15\xff480 passwordicon.py\r\n27.08.2020  10:38    <DIR>          recordings\r\n21.08.2020  13:32             3\xff713 recording_app.zip\r\n26.08.2020  19:54               710 video_encoder.py\r\n               6 \xe4\xa0\xa9\xab\xae\xa2      1\xff450\xff250 \xa1\xa0\xa9\xe2\r\n               3 \xaf\xa0\xaf\xae\xaa  600\xff902\xff475\xff776 \xa1\xa0\xa9\xe2 \xe1\xa2\xae\xa1\xae\xa4\xad\xae\r\n'

Как мне получить результат выполнения системной команды в нормальном (в utf-8) виде?


PS OS Windows 10

0

1 ответ 1

6

Команды оболочки в русских версиях Windows выводят результат в кодировке cp866:

x = b' \x92\xae\xac \xa2 \xe3\xe1\xe2\xe0\xae\xa9\xe1\xe2\xa2\xa5 C \xad\xa5 \xa8\xac\xa5\xa5\xe2 \xac\xa5\xe2\xaa\xa8.\r\n \x91\xa5\xe0\xa8\xa9\xad\xeb\xa9 \xad\xae\xac\xa5\xe0 \xe2\xae\xac\xa0: 3858-A3CB\r\n\r\n \x91\xae\xa4\xa5\xe0\xa6\xa8\xac\xae\xa5 \xaf\xa0\xaf\xaa\xa8 C:\\Users\\User\\Desktop\\soft_and_code\\ph-dl\\kivy\\shareit\\filesharerkivy\\filesharekivy v1.3\\project\\optimazer 1\r\n\r\n27.08.2020  15:27    <DIR>          .\r\n27.08.2020  15:27    <DIR>          ..\r\n27.08.2020  15:27             1\xff157 cursor_info.py\r\n27.08.2020  10:41           713\xff820 output.mkv\r\n27.08.2020  10:42           715\xff370 output.mp4\r\n26.08.2020  19:54            15\xff480 passwordicon.py\r\n27.08.2020  10:38    <DIR>          recordings\r\n21.08.2020  13:32             3\xff713 recording_app.zip\r\n26.08.2020  19:54               710 video_encoder.py\r\n               6 \xe4\xa0\xa9\xab\xae\xa2      1\xff450\xff250 \xa1\xa0\xa9\xe2\r\n               3 \xaf\xa0\xaf\xae\xaa  600\xff902\xff475\xff776 \xa1\xa0\xa9\xe2 \xe1\xa2\xae\xa1\xae\xa4\xad\xae\r\n'
 
print(x.decode('cp866'))

Результат:

 Том в устройстве C не имеет метки.
 Серийный номер тома: 3858-A3CB

 Содержимое папки C:\Users\User\Desktop\soft_and_code\ph-dl\kivy\shareit\filesharerkivy\filesharekivy v1.3\ingproject\optimazer 1

27.08.2020  15:27    <DIR>          .
27.08.2020  15:27    <DIR>          ..
27.08.2020  15:27             1 157 cursor_info.py
27.08.2020  10:41           713 820 output.mkv
27.08.2020  10:42           715 370 output.mp4
26.08.2020  19:54            15 480 passwordicon.py
27.08.2020  10:38    <DIR>          recordings
21.08.2020  13:32             3 713 recording_app.zip
26.08.2020  19:54               710 video_encoder.py
               6 файлов      1 450 250 байт
               3 папок  600 902 475 776 байт свободно

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.