我注意到这种奇怪的行为在python-我试图记录一个进程的输出,然后读取这个输出,并对它做一些处理。即使该文件在程序运行后打开时也具有所有文本,但我无法读取任何内容。奇怪的IO行为与子进程
它的那样简单
f=open("blah.txt",'w')
#I log the output of a program with subprocess
Cmdline="program.exe"
Dump= subprocess.Popen(CmdLine,stdout=f,stderr=subprocess.STDOUT)
#Waiting for it to finish
while(Dump.poll() is not None): #returns None while subprocess is running
print "waiting on process to finish \n"
f.flush() #I flush everything to make sure it was written
sys.stdout.flush()
f.close()
#now i need to read from this file
f= open("blah.txt", 'r')
line=f.readline()
while line:
print line
line=f.readline()
f.close()
我看了绝对没问题,但是当我运行该程序后打开文件blah.txt,一切都在那里。任何暗示我可能做错了什么?从“等待过程到完成”我没有得到任何印刷品,但该过程需要一秒左右的时间才能完成。
什么是'f'?不应该是'f = open(...'? – Blender 2013-03-26 00:04:15
对不起,错字,修正。这不是我的程序中的问题。 – Illusionist 2013-03-26 00:04:50
@Illusionist有很多地方需要显示这不是程序你正在运行,请尽可能少地修改[* actual * program](http://sscce.org/),否则错误可能在其他地方。例如,[此演示程序](https ://gist.github.com/phihag/5242061)在我的系统上工作正常 – phihag 2013-03-26 00:10:08