[英]Extracting stderr from pexpect
My question is simple: Can I expect()
to see certain output on stderr using pexpect? 我的问题很简单:我可以
expect()
使用pexpect在stderr上看到某些输出吗? It seems pexpect.spawn()
can only be used to expect output on stdout. 看来
pexpect.spawn()
仅可用于期待stdout上的输出。
Utopian example: 乌托邦式的例子:
import pexpect child = pexpect.spawn(...) child.expect("hi", fd=pexpect.STDERR)
Or in prose, "expect the string 'hi' on stderr". 或在散文中,“请在stderr上使用字符串'hi'”。
I have not found any mention of such a facility in the docs, but I do note that the child
instance has a stderr
attribute... 我没有在文档中找到任何提及此类功能的信息,但我确实注意到该
child
实例具有stderr
属性...
A hack which semi-achieves what I want is to redirect stderr to stdout in the spawn arguments, then we can use regular expect()
. 一种半实现我想要的技巧是在spawn参数中将stderr重定向到stdout,然后我们可以使用常规的
expect()
。 There must be a better way? 肯定有更好的办法?
Cheers 干杯
For posterity, and based on the comment by Thomas K , this seems to do what you want: 为了后代,并根据Thomas K的评论,这似乎可以满足您的要求:
import os
import subprocess
from pexpect import fdpexpect
program = ['/path/to/command', '--arg1', 'value1', '--arg2', 'value2']
devNull = open(os.devnull, 'w')
command = subprocess.Popen(program, stdout=devNull,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
child = fdpexpect.fdspawn(command.stderr)
child.expect('hi')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.