从pexp中提取stderr

2024-09-26 22:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题很简单:我可以使用pexpect查看stderr上的某些输出吗?似乎pexpect.spawn()只能用于期望stdout上的输出。在

乌托邦式的例子:

import pexpect child = pexpect.spawn(...) child.expect("hi", fd=pexpect.STDERR)

或者在散文中,“期待stderr上的字符串‘hi’”。在

我在文档中没有发现任何关于这种工具的提及,但是我确实注意到child实例有一个stderr属性。。。在

semi实现我想要的一种方法是在spawn参数中将stderr重定向到stdout,然后我们可以使用正则的expect()。一定有更好的办法吗?在

干杯


Tags: 工具字符串文档importchildstderrstdouthi
1条回答
网友
1楼 · 发布于 2024-09-26 22:53:20

对于后代来说,根据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')

相关问题 更多 >

    热门问题