Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess stdout to file, missing new line

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 enter image description here

like image 990
Maud Avatar asked Nov 08 '22 20:11

Maud


1 Answers

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"));
like image 55
Jean-François Fabre Avatar answered Nov 15 '22 05:11

Jean-François Fabre