Output of pexpect

Lie Ryan lie.1296 at gmail.com
Wed Oct 1 09:57:16 CEST 2008

On Tue, 30 Sep 2008 20:48:12 -0700, Anh Khuong wrote:

> I am using pexpect and I want to send output of pexpet to both stdout
> and log file concurrently. Anybody know a solution for it please let me
> know.

One way is to create a file-like object that forked the output to stdout 
and the logfile.

class forkwriter(object):
    def __init__(self, filename):
        self.file = open(filename, 'w')
    def write(self, s):

I've never used pexpect myself though, but if pexpect write things to the 
to the stdout, then you can redirect stdout

import sys
sys.stdout =  forkwriter('mylog.log')

(note: the forkwriter class would have to be modified a bit to use the 
"background" sys.__stdout__ instead of sys.stdout)

More information about the Python-list mailing list