Posts: 4,655
Threads: 1,498
Joined: Sep 2016
i want to call a function and capture its print() output. so i was thinking to use the StringIO class. but when the function is ending and closes its output, StringIO destructs (whereas a real file would have its buffers flushed and be saved. i am looking for a better way to read the print output of the function
after it does the close.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,655
Threads: 1,498
Joined: Sep 2016
Apr-24-2017, 09:56 AM
(This post was last modified: Apr-24-2017, 09:59 AM by Skaperen.)
(Apr-24-2017, 06:40 AM)Mekire Wrote: Not enough to just redirect stdout?
but how to redirect it back to the caller? what i am doing now is printing to stdout and piping that to another process that reads it. the other way i gave up on was printing to a file and reading it back. either too many processes or too slow when i have 600+ of these. since both parts are in python and one is a called function, there has to a more efficient way.
if there is a way to make StringIO
not destroy on .close() that would solve it.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,655
Threads: 1,498
Joined: Sep 2016
(Apr-24-2017, 10:10 AM)wavic Wrote: In Python everything is an object.
Save it somewhere temporarily.
import sys
stdout_bak = sys.stdout
# some code
sys.stdout = output_back
i'm assuming you meant for
stdout_bak and
output_back to be the same.
how do i get what the function that is called prints with the
print(file=foo)
function after it is done? i can pass foo (
a python file-like object) to the function. but the function closes it (e,g. it does
foo.close()
). i tried making
foo be a
StringIO object, but that fails because of the close.
the function is in a module in .pyo format.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.