将pexpect的输出重定向到文件

2 投票
1 回答
6668 浏览
提问于 2025-04-16 07:56

我试着把pexpect的输出保存到一个文件里。

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 | grep 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

但是,mylogfile.txt这个文件没有收到任何输出。虽然文件是创建了,但里面是空的。

1 个回答

2

你忘记从子组件那里获取一些东西了 :)
比如可以加上这个:

child.expect(pexpect.EOF)

撰写回答