0

I have a number of executables that I'm running using a python script. The programs all print to an output file using fprintf, as well as writing to the console. Is there a way to intercept the output when they write to the file?

Whether I use Popen or check_output, the programs don't create the file, but the values that they write to the file don't show up in stdout. The output seems to simply disappear, and I only get the data that they write to the console.

It seems like either no one else has this issue, or no one ever needs to capture file output from a subprocess and I'm going a little crazy trying to figure it out.

If it helps, this is the code i'm using to call each exe. x is the file path and qx is what I've imported check_output as.

try:
    output = qx(x, stderr=STDOUT)
    outputFile.write('\t\t' + output + '\n')
except CalledProcessError as ex:
    outputFile.write('\t\t' + str(ex.output) + '\n')
    outputFile.write('\t Program exited with code: ' + str(ex.returncode) + '\n')

Edit: Eheh actually it turns out that the output file isn't simply disappearing into the cold void after all. My bad. Still, is there a way to capture file output from a subprocess?

CWilson
  • 1
  • 1
  • 2
    Ugh, please don't use `qx` to turn Python into pseudo-Perl. – chepner Dec 12 '14 at 20:53
  • Blame the stackoverflow user whose entry I read to get a handle on subprocess in the first place. I don't use perl and thought it was a nice shorthand. – CWilson Dec 12 '14 at 20:58
  • @chepner: [you can blame me](http://stackoverflow.com/questions/12600892/how-do-i-get-all-of-the-output-from-my-exe-using-subprocess-and-popen#comment16995273_12606327). I accept an edit that replaces `qx` with `check_output` though I won't do it myself. – jfs Dec 16 '14 at 16:39

0 Answers0