开发者

redirecting output of pexpect to a file

I tried to capture the output of pexpect in a file.

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> child = pexpect.spawn('iostat 3')
>>> fout = file('mylogfile.txt','w')
>>> child.logfile = fout  
tazim@webdev:~/webexample$ ps -ef | g开发者_如何学JAVArep iostat
tazim     4683  4675  0 12:49 pts/3    00:00:00 /usr/bin/iostat 3
tazim     4685  4560  0 12:51 pts/2    00:00:00 grep --color=auto iostat

However mylogfile.txt does not receives the output. The file is created but is empty.


You have forgotten to expect something from the child :) Add this, for instance:

child.expect(pexpect.EOF)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜