Skip to content Skip to sidebar Skip to footer

Catch Print Output

I’m using opencv and there is a call for video frame reading with VideoCapture and there is print statement automatically printing errors and information on console , and I want

Solution 1:

I dont know if its the best way to do this but it will work.

You can read in everything your program prints into the console by typing this:

Here we print print("test-test-test-test") into the console, like opencv does it, and with p.stdout.readline() you can read it in again.

import os
import sys
from subprocess import Popen, PIPE, STDOUT

script_path = os.path.join('name_of_your_program.py')

p = Popen([sys.executable, '-u', script_path],
          stdout=PIPE, stderr=STDOUT, bufsize=1)

while True:
    print("test-test-test-test")

    string = p.stdout.readline() 
    print(string[0:3])

Output:

test-test-test-test
b'tes'
test-test-test-test
b"b'T"
test-test-test-test
b'tes'

(It reads in binary so you have to convert it to a string.)

Post a Comment for "Catch Print Output"