Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing stdout and stderr
#1
inside a module i want to change where stdout and stderr go to. i had several ideas but nothing worked. i can't find anything about how to do this in the manual. the latest i tried was to change file descriptors 1 and 2 but that only left me with the file i opened being empty.
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
Which stdout/stderr? Changing them by assigning to sys.stdout/sys.stderr should work the same in a module as it does in the main program.

redirect.py
import sys

def redirect(outname, errname):
    sys.stdout = open(outname, "w")
    sys.stderr = open(errname, "w")
    return
Main program that calls the module:
import redirect

print("hello")        # goes to stdout
redirect.redirect("/tmp/redirected.out", "/tmp/redirected.err")
print("hello, there") # should go to redirected stdout
5/0                   # error should go to redirected stderr
Output:
$ python3 test.py hello $ cat /tmp/redirected.out hello, there $ cat /tmp/redirected.err Traceback (most recent call last): File "test.py", line 6, in <module> 5/0 # error should go to redirected stderr ZeroDivisionError: division by zero
Reply
#3
(Nov-30-2020, 03:54 AM)bowlofred Wrote: Which stdout/stderr?
both!

i tried something like your redirect.py, replacing them with the opened file reference but nothing was output. i concluded they were cached somewhere. i even tried closing them in advance.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
By which, I was trying to make sure you were talking about the stdout/stderr of the python process and not of some calling shell or called subprocess.

Is the console still getting the data? If not, then the redirection has worked. Need to figure out why it's not being written. Have you tried with unbuffered output (like running with -u or putting a flush into the print() function?)
Reply
#5
i redirected to a file and did output. i got nothing on the console and an empty file. i concluded that it did something but not the right thing. i should have gotten the output in the file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [subprocess] Why stdout sent to stderr? Winfried 3 496 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Performance options for sys.stdout.writelines dgrunwal 11 3,164 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  stderr redirect to file fmr300 2 3,602 Apr-03-2021, 01:31 AM
Last Post: fmr300
  Get stdout of a running process yok0 0 3,040 Aug-20-2020, 10:12 AM
Last Post: yok0
  connot write to sys.stderr anne 7 2,884 Jul-31-2020, 12:39 AM
Last Post: millpond
  how to output to stderr from inside a generator? Skaperen 2 1,843 Mar-03-2020, 03:17 AM
Last Post: Skaperen
  will with sys.stdout as f: close sys.stdout? Skaperen 9 4,634 Nov-03-2019, 07:57 AM
Last Post: Gribouillis
  Add stdout to text file maxtimbo 3 3,171 Feb-05-2019, 12:53 AM
Last Post: maxtimbo
  stdout buffering Skaperen 5 4,743 Jun-12-2018, 06:14 AM
Last Post: Skaperen
  stdout Skaperen 0 2,655 Mar-19-2018, 01:23 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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