我正在调用一个子进程,我希望子进程的输出写入到一个已经打开的文件中。我正在使用以下代码:
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()
这在我的程序中可以正常工作,因为我从.exe获取实时输出。但是,所有输出都写在一行中。作为独立文件,输出会显示新行。 我尝试使用universal_newlines或p.communicate(),但没有成功。 编辑1: Windows 10 Python版本2.7.13 编辑2: 十六进制文件
\n\r
作为行结尾,但Python只写入\n
。 - Maurice Meyer\r\n
。 - Jean-François Fabre