Alex Alex - 5 months ago 16
Python Question

How to start a command with subprocess with output in some file, unblocked?

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...

Answer

sys.stdout is buffered by default. You can disable it by passing -u to python.

import subprocess
cwd = "/home/path/to/the/executable"
cmd = "python -u my_code.py --arg1"
with open('output.txt', "w") as outfile:
    subprocess.Popen(cmd.split(), stdout=outfile, cwd = cwd)