Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get return value of c++ int main() output using subprocess.check_output() in python script

Hi I am using a C++ script as follows EX:

int main(){
 int x =7;
 std::cout<<"print num:"<<x<<std::endl;
 if(x>0){
  std::cout<<" good"<<std::endl;
  return 5;
 }
 return 0;
}

For this in my python file I am calling the subprocess as follows:

result0_1 = subprocess.check_output(MYCPP_fILE_PATH,shell =True)
print(result0_1.decode("utf-8"))

It prints out "print num:" and "good" but dosn’t provide me the return value "5".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I get the return value but not the std::cout from the cpp file?

>Solution :

From Python 3 docs

And very similar Python 2 docs

If the return code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and any output in the output attribute.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading