我该如何让subprocess.check_call命令返回一个命令的原始二进制输出?它似乎在某个地方对其进行了错误编码。
细节:
我有一个返回以下文本的命令:
some output text “quote” ...
以下是我如何调用该命令的方式:
f_output = SpooledTemporaryFile()
subprocess.check_call(cmd, shell=True, stdout=f_output)
f_output.seek(0)
output = f_output.read()
问题是我得到了这个:
>>> repr(output)
some output text ?quote? ...
>>> type(output)
<str>
(如果我将'ord'称为'?',我会得到63。)我在Linux上使用Python 2.7。请注意:在OSX上运行相同的代码对我来说是正确的。问题出现在我在Linux服务器上运行它时。
?
,它在被读取的文件中。因此,程序没有写入您认为它写入的字符串。 - tdelaneyf_output = SpooledTemporaryFile();subprocess.check_call('echo -e \'some output text \\xe2\\x80\x9cquote\\xe2\\x80\\x9d ...\'', shell=True, stdout=f_output);f_output.seek(0);output=f_output.read();print(repr(output));
将得到'some output text \xe2\x80\x9cquote\xe2\x80\x9d ...\n'
。 - v7d8dpo4