Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get print() output
#1
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.
Reply
#2
Not enough to just redirect stdout?
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(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.
Reply
#5
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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()
Reply
#7
(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.
Reply
#8
In Python everything is an object.
Save it somewhere temporarily.

import sys

stdout_bak = sys.stdout

# some code

sys.stdout = stdout_back
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(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.
Reply
#10
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,076 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,323 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,280 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,910 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,821 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 11,074 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,921 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,110 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 4,034 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Output with none, print(x) in function Vidar567 3 2,505 Nov-24-2020, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020