Python Forum
setting STDOUT and/or STDERR
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setting STDOUT and/or STDERR
#1
the script being developed will basically be like a shell script, running many command with some logic between many of them. at the start of the script i want to redirect STDOUT and/or STDERR to output to a specified file, to record all the output from all the commands. in bash, i can do that with this line in the script:
exec &>/tmp/bigscript.log
using my past C experience i came up with this Python3 code:
import os
fd = os.open('/tmp/bigscript.log',os.WRONLY|os.O_CREAT|os.O_TRUNC,mode=0o600)
if fd!=0:os.dup2(fd,0)
if fd!=1:os.dup2(fd,1)
os.close(fd)
is this code good enough or is there a more expected (and pythonic) way to do this?
Tradition is peer pressure from dead people

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


Messages In This Thread
setting STDOUT and/or STDERR - by Skaperen - Dec-05-2023, 07:10 AM
RE: setting STDOUT and/or STDERR - by Gribouillis - Dec-05-2023, 10:14 AM
RE: setting STDOUT and/or STDERR - by DeaD_EyE - Dec-05-2023, 01:06 PM
RE: setting STDOUT and/or STDERR - by Gribouillis - Dec-05-2023, 03:20 PM
RE: setting STDOUT and/or STDERR - by DeaD_EyE - Dec-07-2023, 04:56 AM
RE: setting STDOUT and/or STDERR - by Gribouillis - Dec-07-2023, 06:21 AM
RE: setting STDOUT and/or STDERR - by Skaperen - Dec-07-2023, 06:32 PM
RE: setting STDOUT and/or STDERR - by Gribouillis - Dec-08-2023, 08:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  combining stdout and stderr Skaperen 1 1,807 Nov-01-2019, 07:06 AM
Last Post: Gribouillis
  capture stdout from child processes Skaperen 0 3,365 Oct-30-2019, 12:11 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