Может быть, вы можете перенаправить sys.stdout
в буфер?
import sys
from io import StringIO
stdout_backup = sys.stdout
sys.stdout = string_buffer = StringIO()
# this function prints something and I cannot change it:
def my_function():
print('Hello World')
my_function() # <-- call the function, sys.stdout is redirected now
sys.stdout = stdout_backup # restore old sys.stdout
string_buffer.seek(0)
print('The output was:', string_buffer.read())
Печать:
The output was: Hello World