I am calling a subprocess, I want the subprocess output written to an already open file. I am using the following code:
f1=open('solve.out','w')
#beginning of the programm writes to this file
f_err = open('mor.err', "w")
arguments=[file.exe,arg1,arg2,...]
p=subprocess.Popen(arguments,stdout=f1, stderr=f_err)
p.wait()
f1.close()
f_err.close()
This works fine as I get the realtime output from the .exe in my program. However, the outputs are all written in one single line. As standalone, the output appears with new lines.
I tried universal_newlines or the p.communicate() without success.
edit 1 : windows10 Python version 2.7.13
edit 2 : Hex file
your program seems to detect that the output is being redirected to a non-console output.
There are ways to make believe to the program that it's writing to a console, but in your case there may exist a simple workaround which would be to replace 6 spaces by a linefeed. Of course this isn't perfect if spaces occur somewhere else, but that's a start.
For this you would have to first redirect to 2 separate pipes (to avoid reading/writing a file on disk), replace the spaces, then write to file:
p=subprocess.Popen(arguments,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,error = p.communicate()
f1.write(output.replace(" "*6,"\n"));
f_err.write(error.replace(" "*6,"\n"));
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