I tried such code to redirect standart output to the file:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout.txt, stderr=stdout.txt)
But got error: NameError: name 'stdout' is not defined
I use python version 2.5.2
Open the file first and use a
to append if you want to keep a record of all output/errors or use w
to overwrite each time:
with open("stdout.txt","a+") as stdout:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout, stderr=stdout)
Using with
will automatically close your file.
Give a file descriptor to stdout See doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With