Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带stdout重定向并返回int的Python子进程 我试图从一个正在使用子过程运行的C++程序中的一组打印语句中读取数据。_Python_Subprocess_Stdout_Communicate - Fatal编程技术网

带stdout重定向并返回int的Python子进程 我试图从一个正在使用子过程运行的C++程序中的一组打印语句中读取数据。

带stdout重定向并返回int的Python子进程 我试图从一个正在使用子过程运行的C++程序中的一组打印语句中读取数据。,python,subprocess,stdout,communicate,Python,Subprocess,Stdout,Communicate,C++代码: printf "height= %.15f \\ntilt = %.15f \(%.15f\)\\ncen_volume= %.15f\\nr_volume= %.15f\\n", height, abs(sin(tilt*pi/180)*ring_OR), abs(tilt), c_vol, r_vol; e; //e acts like a print Python代码: run = subprocess.call('Name', stdout = subprocess.PI

C++代码:

printf "height= %.15f \\ntilt = %.15f \(%.15f\)\\ncen_volume= %.15f\\nr_volume= %.15f\\n", height, abs(sin(tilt*pi/180)*ring_OR), abs(tilt), c_vol, r_vol; e; //e acts like a print
Python代码:

run = subprocess.call('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()
但是,我得到的不是数据,而是一个int,即退出代码,或者是0,或者是一个错误代码。当然,python会告诉我“AttributeError:'int'对象没有属性'communicate'”

如何实际获取数据(printf)?

只需运行命令并返回其退出状态(在python中,退出状态可以通过
sys.exit(N)
——在其他语言中,退出状态通过不同的方式确定)。如果您想实际获得进程的句柄,则需要使用
subprocess.Popen
。以你为例:

run = subprocess.Popen('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()
程序退出状态现在可通过
returncode
属性获得

此外,就风格而言,我会:

run = subprocess.Popen('Name', stdout = subprocess.PIPE, stderr = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()
或:

既然你没有给自己捕捉stderr的能力,你可能不应该假装你得到了一些有意义的东西

run = subprocess.Popen('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, _ = run.communicate()