Python decoding question. UnicodeDecodeError 'utf-8' codec can't decode byte 0xb5 in position 3

Asked 1 years ago, Updated 1 years ago, 108 views

I'm making a shell out of Python.

import os
from subprocess import check_output
print(check_output("dir", shell=True).decode('utf-8'))

It is a program that returns and prints the returned value when you enter the cmd command using the subprocess module.

It works well if you enter a command with only English return values, but if you use a command mixed with Korean, for example, the dir command, UnicodeDecodeError occurs.

The error name is as follows 'utf-8' codec can't decode byte 0xb5 in position 3: invalid start byte

If the return value is mixed with byte and utf-8 like this, how should I decode it? ㅠ<

python decoding unicodedecodeerror

2022-09-21 16:26

1 Answers

It's not a byte problem, but it's using euc-kr when encoding Korean in Windows (unlike other os).

Fix it like the bottom.

print(check_output("dir", shell=True).decode('euc-kr'))


2022-09-21 16:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.