David David - 4 years ago 116
Python Question

os.popen().read() - charmap decoding error

This is NOT a duplicate. The error is similiar, but the code is completly different and unrelated. There is no way I could use the answers from the other questions to solve this problem. There is no answer to this question on stackoverflow yet.

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. I've looked through some questions that are supposed to be similiar to mine, but they are all about reading files.

Answer Source

os.popen is just a wrapper around subprocess.Popen along with a io.TextIOWrapper object:

The returned file object reads or writes text strings rather than bytes.

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:

p = subprocess.Popen('cmd /u /c dir', stdout=subprocess.PIPE)
result = p.communicate()
text = result[0].decode('u16')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download