I am trying to use subprocess
to start a command on the command-line for which I want to have the output written into some file. So, in other words, I want to have e.g. the following command executed within python:
python my_code.py --arg1 > output.txt
I tried the following:
import subprocess
cwd = "/home/path/to/the/executable"
cmd = "python my_code.py --arg1"
with open('output.txt', "w") as outfile:
subprocess.Popen(cmd.split(), stdout=outfile, cwd = cwd)
but the output file stayed empty. How to else do it (not blocking!)?
Addition:
My guess is that the output file is created, but is being closed as soon as the above code finishes. Therefore, no output to that file...