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_run and stdout flux paul18fr 2 514 Jan-09-2025, 08:50 PM
Last Post: Gribouillis
  Failing to iterate over captured StdOut tester_V 4 1,467 Jul-12-2024, 03:36 PM
Last Post: deanhystad
  [subprocess] Why stdout sent to stderr? Winfried 3 2,002 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Performance options for sys.stdout.writelines dgrunwal 11 6,019 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  stderr redirect to file fmr300 2 5,235 Apr-03-2021, 01:31 AM
Last Post: fmr300
  Get stdout of a running process yok0 0 4,366 Aug-20-2020, 10:12 AM
Last Post: yok0
  connot write to sys.stderr anne 7 4,083 Jul-31-2020, 12:39 AM
Last Post: millpond
  how to output to stderr from inside a generator? Skaperen 2 2,408 Mar-03-2020, 03:17 AM
Last Post: Skaperen
  will with sys.stdout as f: close sys.stdout? Skaperen 9 6,990 Nov-03-2019, 07:57 AM
Last Post: Gribouillis
  Add stdout to text file maxtimbo 3 3,986 Feb-05-2019, 12:53 AM
Last Post: maxtimbo

Forum Jump:

User Panel Messages

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