0

I have a piece of code as below:

test_process = subprocess.Popen(['python', self.bin_dir + '/' + self.bin_name] + arg_list,
                                            cwd=os.path.normpath(cwd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
o = test_process.communicate()[0]
o = (o or b'')
sys.stdout.write(o.decode("utf-8"))

On windows8 machine when I run this piece of code with python3, it gives such error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 898229: invalid start byte This code works on Linux environment, I tried adding encoding='utf8' to the Popen call but that won't solve the issue, current thought is that Windows does not use utf-8 as Popen stdout, however not have an idea to fix this issue, any idea?

Boooooo
  • 157
  • 3
  • 12
  • The encoding needed depends on how the executable was built. A C executable using `printf()` to write `char*` strings will use whatever encoding the source file was written in. Linux typically uses UTF-8 sources, which is why it works for you in Linux. – Mark Tolonen Jan 18 '23 at 06:20
  • Does this answer your question? [Python: How to decode file names retrieved from 'dir' command using subprocess?](https://stackoverflow.com/questions/67524114/python-how-to-decode-file-names-retrieved-from-dir-command-using-subprocess) – JosefZ Jan 18 '23 at 15:33

0 Answers0