How can I get subprocess.check_call to give me the raw binary output of a command, it seems to be encoding it incorrectly somewhere.
Details:
I have a command that returns text like this:
some output text “quote” ...
(Those quotes are unicode e2809d)
Here's how I'm calling the command:
f_output = SpooledTemporaryFile()
subprocess.check_call(cmd, shell=True, stdout=f_output)
f_output.seek(0)
output = f_output.read()
The problem is I get this:
>>> repr(output)
some output text ?quote? ...
>>> type(output)
<str>
(And if I call 'ord' the '?' I get 63.) I'm on Python 2.7 on Linux.
Note: Running the same code on OSX works correctly to me. The problem is when I run it on a Linux server.
SpooledTemporaryFileis over kill. The "spooled" part only works for stuff written from python. When you got the file descriptor it changed it to a regular temporary file. The extra StringIO buffer wasn't used.viminstalled you should also havexxd, which can display a file hex dump. In your example text, the utf-8 output should look like:0000000: 736f 6d65 206f 7574 7075 7420 7465 7874 some output text 0000010: 20e2 809c 7175 6f74 65e2 809d 202e 2e2e ...quote... ...The left quote ise2 80 9cand the right quote ise2 80 9d?, it was in the file being read. So, the program didn't write the string you think it did.