Dec-08-2023, 08:38 AM
(This post was last modified: Dec-08-2023, 08:38 AM by Gribouillis.)
(Dec-07-2023, 06:32 PM)Skaperen Wrote: i want to have this more integrated so that the redirection is done by the script itself and minimize the number of filesThe more pythonic way could be to use @DeaD_EyE 's suggestion plus
subprocess.check_output()
or Popen.communicate()
. Here is a 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) # run some command which creates output data = sp.check_output(['date'], stderr=sp.STDOUT, encoding='utf8') print(data, end='') with Path("/tmp/bigscript.log").open("w") as ofile: with redirect_stdout(ofile), redirect_stderr(ofile): main()
Output:λ cat /tmp/bigscript.log
['paillasse/pf/myscript.py']
ven. 08 déc. 2023 09:38:16 CET
λ
« We can solve any problem by introducing an extra level of indirection »