Saving stdout from subprocess.Popen to file, plus writing more stuff to the file Saving stdout from subprocess.Popen to file, plus writing more stuff to the file python python

Saving stdout from subprocess.Popen to file, plus writing more stuff to the file


You could call .wait() on each Popen object in order to be sure that it's finished and then call log.flush(). Maybe something like this:

def run(cmd, logfile):    p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=logfile)    ret_code = p.wait()    logfile.flush()    return ret_code

If you need to interact with the Popen object in your outer function you could move the .wait() call to there instead.


You need to wait until the process is finished before you continue. I've also converted the code to use a context manager, which is cleaner.

def run(cmd, logfile):    p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=logfile)    p.wait()    return pdef runTest(path, flags, name):    with open(name, "w") as log:        print >> log, "Calling executable A"        a_ret = run(path + "executable_a_name" + flags, log)        print >> log, "Calling executable B"        b_ret = run(path + "executable_b_name" + flags, log)        print >> log, "More stuff"


I say just keep it real simple. Pseudo code basic logic:

write your start messages to logAexecute A with output to logAwrite your in-between messages to logBexecute B with output to logBwrite your final messages to logBwhen A & B finish, write content of logB to the end of logAdelete logB