Python Forum
get print() output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: get print() output (/thread-3000.html)

Pages: 1 2


get print() output - Skaperen - Apr-24-2017

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.


RE: get print() output - Mekire - Apr-24-2017

Not enough to just redirect stdout?


RE: get print() output - wavic - Apr-24-2017

I think he is doing just that.
Or maybe not. Are you using the print() file argument to redirect the output? Also, print() has a boolean flush argument. I am not really sure what it does.
You can pass the StringIO object to the function and use it as print(text, file=file_obj) argument


RE: get print() output - Mekire - Apr-24-2017

(Apr-24-2017, 06:57 AM)wavic Wrote: Are you using the print() file argument to redirect the output?
I had assumed it was a case where this couldn't be altered. Like a black box function that prints stuff; in which case you would need to redirect stdout with the sys module or similar.


RE: get print() output - wavic - Apr-24-2017

from io import StringIO

def my_func(out_file):
    text = "You won't see me."
    print(text, file=out_file)

output = StringIO()

my_func(output)

out_text = output.getvalue()
I was thinkging for something like this.


RE: get print() output - Mekire - Apr-24-2017

Was thinking this:

import sys


# Some function you can't change
def my_func():
    text = "You won't see me."
    print(text)

 
sys.stdout = open("sample.txt", "w")
my_func()



RE: get print() output - Skaperen - Apr-24-2017

(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.


RE: get print() output - wavic - Apr-24-2017

In Python everything is an object.
Save it somewhere temporarily.

import sys

stdout_bak = sys.stdout

# some code

sys.stdout = stdout_back



RE: get print() output - Skaperen - Apr-25-2017

(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.


RE: get print() output - wavic - Apr-25-2017

(Apr-25-2017, 06:16 AM)Skaperen Wrote: i'm assuming you meant for stdout_bak and output_back to be the same.
Right! I fixed it.

import sys

stdout_bak = sys.stdout

with open('output.txt', 'w') as sys.stdout:
    print("I've never did this before so now I'm experimenting")
    print("After that output.txt should containd this prints")

sys.stdout = stdout_bak # you must return everything as it was before to print normally again

print('After the first with statement')

with open('output.txt', 'r') as in_file:
    print(in_file.read())
Output:
victor@jerry:~$ python3 /tmp/test.py After the first with statement I've never did this before so now I'm experimenting After that output.txt should containd this prints