0

I have an algorithm that is written in C++ that outputs a cout debug statement to the terminal window and I would like to figure out how to read that printout with python without it being piped/written to a file or to return a value.

Python organizes how each of the individual C++ algorithms are called while the data is kept on the heap and not onto disk. Below is an example of the a situation that is of similar output,

+-------------- terminal window-----------------+

(c++)runNewAlgo: Debug printouts on

(c++)runNewAlgo: closing pipes and exiting 

(c++)runNewAlgo: There are 5 objects of interest found

( PYTHON LINE READS THE PRINT OUT STATEMENT)

(python)main.py: Starting the next processing node, calling algorithm

(c++)newProcessNode: Node does work

+---------------------------------------------------+

Say the line of interest is "there are 5 objects of interest" and the code will be inserted before the python call. I've tried to use sys.stdout and subprocess.Popen() but I'm struggling here.

4
  • I don't understand. Your question is about Python, but you tag as C++. Are you mixing the languages? If not, only tag the language you will be using. Commented Dec 28, 2020 at 19:27
  • Will you start the C++ algorithm from within the python script or externally? Would piping the output to python work for you? Commented Dec 28, 2020 at 19:27
  • @ThomasMatthews I was the one that edited in the C++ tag. I did so because the OP wants to read the printout of a C++ program with Python, so therefore he is kind of mixing the languages. Did I do so incorrectly? Commented Dec 28, 2020 at 19:29
  • the languages are mixed in the API that is being used. The C++ algorithm is started from within the python script. Piping may be of use but this is run remotely which may cause a memory backup if not managed correctly. Commented Dec 28, 2020 at 19:34

2 Answers 2

0

Your easiest path would probably be to invoke your C++ program from inside your Python script.

More details here: How to call an external program in python and retrieve the output and return code?

Sign up to request clarification or add additional context in comments.

Comments

0

You can use stdout from the returned process and read it line-by-line. The key is to pass stdout=subprocess.PIPE so that the output is sent to a pipe instead of being printed to your terminal (via sys.stdout).

Since you're printing human-readable text from your C++ program, you can also pass encoding='utf-8' as well to automatically decode each line using utf-8 encoding; otherwise, raw bytes will be returned.

import subprocess

proc = subprocess.Popen(['/path/to/your/c++/program'],
                        stdout=subprocess.PIPE, encoding='utf-8')
for line in proc.stdout:
  do_something_with(line)
  print(line, end='') # if you also want to see each line printed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.