我有一个用C++编写的可执行文件,它将一些json写入标准输出。
比如说在终端上运行它,我有,
PS C:\Users\laerne\Projects\pylol> C:\Users\laerne\Projects\d\master\build\bin\Debug\software.exe --opt-descr
[
{
"name": "",
"options": [
]
},
{
"name": "run"
},
{
"name": "normal-from-mesh"
},
{
"name": "info"
},
{
"name": "world-space-direction"
},
{
"name": "position-from-mesh"
},
{
"name": "ambient-occlusion"
},
{
"name": "bent-normal-from-mesh"
},
{
"name": "ambient-occlusion-from-mesh"
},
{
"name": "curvature-from-mesh"
},
{
"name": "position"
},
{
"name": "normal-world-space"
},
{
"name": "texture-from-mesh"
},
{
"name": "height-from-mesh"
},
{
"name": "thickness-from-mesh"
},
{
"name": "curvature"
},
{
"name": "color-from-mesh"
},
{
"name": "opacity-mask-from-mesh"
},
{
"name": "uv-map"
}
]现在我想从python中读取该json。为此,我调用subprocess来生成json。
subprocess.Popen(
[r"C:\Users\laerne\Projects\d\master\build\bin\Debug\software.exe","--opt-descr"],
stdout=subprocess.PIPE ).stdout.read()但是,这将返回空字符串!尝试使用更大的json文本,我注意到最后几个~200~400个字符总是被删除,我不知道为什么。你能帮我猜猜为什么python顽固地拒绝读最后几个字符吗?
非常感谢!
发布于 2017-08-04 21:34:30
您忘记了等待子进程完成。尝试:
p = subprocess.Popen( [r"C:\Users\laerne\Projects\d\master\build\bin\Debug\software.exe","--opt-descr"],
stdout=subprocess.PIPE)
p.wait()
print(p.stdout.read())或者,您可以尝试使用check_output:
print(subprocess.check_output([r"C:\Users\laerne\Projects\d\master\build\bin\Debug\software.exe","--opt-descr"])编辑:它还有另一个方面--输出数据的应用程序。记住在您的C++应用程序中刷新标准输出。
https://stackoverflow.com/questions/45508048
复制相似问题