如何通过Python获取C程序的返回值

huangapple go评论85阅读模式
英文:

How to get return value by C program in python

问题

我有一个在Windows上运行的C程序,它有一个返回值的主函数。

#include <stdio.h>

int testData()
{
  int testErr = 0;
  // ....

  return(testErr);
}

int main(void) {
  int mainErr = 0;

  mainErr = testData();
  printf("mainerr = %d", mainErr);

  return mainErr;
}

这个C代码可执行文件myTest.exe通过Python运行。

self.run_cmd = "myTest.exe " + cmd  # 传递参数给C可执行文件

self.testRun = subprocess.run(self.run_cmd,
                               stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,
                               bufsize=0, universal_newlines=True, timeout=100)
print(">>>>", self.testRun.stdout)
print(">>>>", self.testRun.stderr)

使用print(">>>>", self.testRun.stdout),我在Python中没有得到C代码的返回值。
在Python中如何获取C main 返回的返回值?

英文:

I have a C(on windows) program which has a main function which returns a value.

#include &lt;stdio.h&gt;    
int testData()
{
  int testErr = 0;    
  // ....

  return(testErr);    
}

int main(void) {
  int mainErr = 0;

  mainErr = testData();  
  printf(&quot;mainerr = %d&quot;, mainErr);

  return mainErr;
}

This C code executable myTest.exe is run through python.

self.run_cmd = &quot;myTest.exe &quot;+cmd           # pass agruments to the C exe

    self.testRun = subprocess.run(self.run_cmd,
                                   stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,
                                   bufsize=0, universal_newlines=True, timeout=100)
    print(&quot;&gt;&gt;&gt;&gt;&quot;, self.testRun.stdout)
    print(&quot;&gt;&gt;&gt;&gt;&quot;, self.testRun.stderr)

Using "print(">>>>", self.testRun.stdout)" I am not getting the C code's return value in python.
In python how to get the return value returned by C main.

答案1

得分: 2

run 函数返回一个 CompletedProcess 对象,该对象具有一个 returncode 属性。

英文:

The run function returns a CompletedProcess object, which have a returncode property.

答案2

得分: 0

在Python中,subprocess模块监视命令的返回值(即成功为0,失败为1)。由于您从C程序返回了某个值,因此程序成功退出并返回了返回代码0。在这里,您可以在C程序中使用exit(1)来终止程序并返回退出代码1。

可以使用以下方式监视C程序的退出代码:

import subprocess

subproc = subprocess.run(<要执行的命令>, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if subproc.returncode == 0:
    logger.info('成功退出')

另一种方法是将所需的输出写入文件或数据库,然后从Python代码中读取。

英文:

In python subprocess module monitors the return value of the command (i.e. 0 for success and 1 for failure). Since you are returning some value from your C program the program successfully exited with return code 0. What you can do here is you can use exit(1) in your C program to terminate the program with exit code 1 where you want to return 1.
The exit code of the C program can be monitored using,

subproc = subprocess.run(&lt;command to execute&gt;, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
if (subproc.returncode == 0):
    logger.info(&#39;Exited successfully&#39;)

Other way is to write your desired output in some files or DB and read that from your python code.

答案3

得分: 0

使用子进程获取的返回值将是字节。
运行后:

self.testRun = subprocess.run(self.run_cmd, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,  bufsize=0, universal_newlines=True, timeout=100)

您可以捕获输出:

result = self.testRun.stdout.decode('utf-8')

此结果是一个字符串,可以进一步使用。

英文:

The return value obtained from using subprocess will be bytes.
After running :

self.testRun = subprocess.run(self.run_cmd, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,  bufsize=0, universal_newlines=True, timeout=100)

You can capture the output as :

result = self.testRun.stdout.decode(&#39;utf-8&#39;)

This result is a string and can be used further.

huangapple
  • 本文由 发表于 2020年1月3日 15:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定