Dec-05-2023, 07:10 AM
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.logusing 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?