I have a bit of an issue here. I have a Python script which calls binaries compiled from C++. The Python script has its own set of outputs (to standard out and error), which are easily disable-able. The C++ binaries have their own set of outputs (to standard out and error, among others) as well; the source can be altered, but I am not the original author. This is an issue because I do not want the C++ output in my final program, and I also don’t want future users to need to edit the C++ source.
What I’d like to be able to do is have some Python method which will catch the C++ code’s output that is sent to standard out or error. Is this possible? If so, could someone point me in the right direction?
Thank you!!
One way to do this is:
stdoutandstderrusingos.dup.stdoutandstderrusingreopen(from C’sstdio) to write to a file of your choice.Note:
reopenisn’t available directly from python, but you should be able to call it as in the example below or using any other wrapper available.After this is done:
coutandcerrin C++ will write to the output files.printstatement in python will write to the output files.However, since the original descriptors are duplicated, you can still (see example below):
stdout/stderrusingsdout.writeandstdout.errloggingmethods after configuring properly thestreamparameterThe following code uses instant library to test real C++ code that is wrapped into python using SWIG and that should be similar to the library that you have:
The output for this example is:
Note: First time the code is executed, you might get
gcccompilation messages (I’ve removed them to make the example clearer).