I have already read UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>. While the error message is similar, the code is completely different, because I use os.popen
in this question, not open
. I cannot use the answers from the other questions to solve this problem.
output = os.popen("dir").read()
This line, which is supposed to assign the output of command "dir" to variable "output", is causing this error:
'charmap' codec can't decode byte 0x88 in position 260: character maps to <undefined>
I think this might be happenning because some files in the folder contain letters such as ł, ą, ę and ć in their names. I have no idea how to fix this though.
In this case, using
subprocess.Popen
is too general, too verbose and too hard to remember. Usesubprocess.check_output
instead.It returns a
bytes
object, which can be converted tostr
withdecode
function.Try it online!
os.popen
is just a wrapper aroundsubprocess.Popen
along with aio.TextIOWrapper
object:If Python's default encoding doesn't work for you, you should use
subprocess.Popen
directly.The underlying issue is that cmd writes ansi garbage by default, even when the output is to a pipe. This behavior may depend on your Windows version.
You can fix this by passing
/U
flag to cmd: