Output of pexpect

Karthik Gurusamy kar1107 at gmail.com
Wed Oct 1 00:32:27 EDT 2008


On Sep 30, 8:48 pm, Anh Khuong <kq... at yahoo.com> wrote:
> Hi all,
>
> 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.

spawn class takes a 'logfile' parameter:
__init__(self, command, args=[], timeout=30, maxread=2000,
searchwindowsize=None, logfile=None, cwd=None, env=None)

More logging info at:
http://pexpect.sourceforge.net/pexpect.html

>From the above link:
The logfile member turns on or off logging. All input and output will
be copied to the given file object. Set logfile to None to stop
logging. This is the default. Set logfile to sys.stdout to echo
everything to standard output. The logfile is flushed after each
write.

Example log input and output to a file::

    child = pexpect.spawn('some_command')
    fout = file('mylog.txt','w')
    child.logfile = fout

Example log to stdout::

    child = pexpect.spawn('some_command')
    child.logfile = sys.stdout

The logfile_read and logfile_send members can be used to separately
log
the input from the child and output sent to the child. Sometimes you
don't want to see everything you write to the child. You only want to
log what the child sends back. For example::

    child = pexpect.spawn('some_command')
    child.logfile_read = sys.stdout

To separately log output sent to the child use logfile_send::

    self.logfile_send = fout

I am not very sure if you can do the logging to two different files at
the same time (ie sys.stdout as well as another file-object). I guess
that's your question. The above should give you a starting point to
explore. [May be give a fake file like object and intercept the write/
flush calls?]

Karthik

>
> Thanks




More information about the Python-list mailing list