Python自定义标准输出过滤器

时间:2014-03-19 10:31:09

标签: python stdout

我想知道是否可以过滤python脚本的标准输出。它使用子进程[*] 1调用另一个脚本(ksh),并在每次执行某些操作时将消息写入控制台。我想要压缩那条消息,它总是以子串foo开始。

我该怎么做?

感谢您的时间。

编辑[*] 1 stdout将其重定向到subprocess.PIPE

编辑方法的代码和免责声明:我知道shell=True非常气馁,但我无法使其工作。此方法调用ksh自定义shell,该shell调用另一个名为nexec

的命令
def runNexec(self, command='pwd'):
    args=['nsh', '-c', 'nexec ' + self.name +' '+ command,'shell=True']

    p = Popen(args, stdout=PIPE, stderr=PIPE)
    (out, err) = (p.communicate())

    status = p.poll()

    if status != 0:
        return (1,log.log_error(err))
    else:
        return (0,out.splitlines())

0 个答案:

没有答案