Dec-07-2023, 06:21 AM
(This post was last modified: Dec-07-2023, 06:21 AM by Gribouillis.)
(Dec-07-2023, 04:56 AM)DeaD_EyE Wrote: It could work, if you set stderr and stdout to subprocess.PIPE.I tried it but it doesn't work. Here is my script
# myscript.py from contextlib import redirect_stdout, redirect_stderr from pathlib import Path import subprocess as sp import sys def main(): # print our args print(sys.argv) sys.stdout.flush() # run some command which creates output sp.run(["date"], stdout=sp.PIPE, stderr=sp.PIPE) # OUTPUT IS LOST with Path("/tmp/bigscript.log").open("w") as ofile: with redirect_stdout(ofile), redirect_stderr(ofile): main()
Output:λ python paillasse/pf/myscript.py
λ cat /tmp/bigscript.log
['paillasse/pf/myscript.py']
λ
I don't know where the output of sp.run(["date"], stdout=sp.PIPE, stderr=sp.PIPE)
was sent.On the other hand IT WORKS if I call instead explicitly
sp.run(["date"], stdout=sys.stdout, stderr=sys.stderr)This could be the pythonic way to go, but I don't think I could do this and redirect to an abstract file object without fileno, in order to make a tee for example.
« We can solve any problem by introducing an extra level of indirection »