0

My script uses an external module which prints to the console some information when something happens, I need to capture the moment this new information has been printed, so in other words, I need to access the current console text and check if this information has been printed out.

after searching a bit online I found this:

old_stdout = sys.stdout
sys.stdout = buffer = StringIO()
# Code
buffer.getvalue()

But for some reason this doesn't capture the console outputs of the module I am using, it only captures the print statements I use, is there a way for me to get the current console text?

Tomergt45
  • 579
  • 1
  • 7
  • 19

2 Answers2

0

If you want to monitor other processes (your external module) output then I would execute this process from monitoring script and then capture its output using subprocess module.

See for example this thread: How can I capture the stdout output of a child process?

Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20
0

Found my answer, apparently, the module I am using is written in C++, and sys.stdout is written only for python, meaning that any changes won't affect modules that are compiled outside of python.

So in order to capture the outputs of the C++ module, I came across this code from another answer:

capture = py.io.StdCaptureFD()
# ...
out, err = capture.readouterr()

which works as expected.

Tomergt45
  • 579
  • 1
  • 7
  • 19