[Tutor] how to get response from os.system()

Martin Walsh mwalsh at groktech.org
Mon Mar 17 14:38:22 CET 2008


Hi Nathan,

Nathan McBride wrote:
> Yup I use the pexpect module for a lot however couldn't get 'pexpect.run' to work with mysqldump piping to gzip 
> 

Just to hazard a guess -- when you want to pipe commands with pexpect
you have to spawn ('run', it seems, would work the same way) the shell
command as an argument to bash (or similar) since pexpect does not
natively interpret shell operators or wildcards, like redirect, pipe, etc...

from http://pexpect.sourceforge.net/pexpect.html
"""
Remember that Pexpect does NOT interpret shell meta characters such as
redirect, pipe, or wild cards (>, |, or *). This is a common mistake.
If you want to run a command and pipe it through another command then
you must also start a shell. For example::

child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
child.expect(pexpect.EOF)
"""

HTH,
Marty

> -----Original Message-----
> From: Jeff Younker <jeff at drinktomi.com>
> Sent: Sunday, March 16, 2008 6:59 PM
> To: Nathan McBride <nomb85 at comcast.net>
> Cc: tutor at python.org
> Subject: Re: [Tutor] how to get response from os.system()
> 
> 
>> Would you mind perhaps show an example running an interactive  
>> command like su and show how to send input to the commands waiting  
>> propmts?
> 
> If you're doing that then you *really* want to be using the pexpect
> module.
> 
> cmd = pexpect.spawn('su - SOMEINTERACTIVECOMMAND')
> cmd.expect('# ')   # the prompt
> cmd.sendline('A COMMAND')
> cmd.expect('# ')   # wait for the prompt again
> output = cmd.before  # the stuff before the prompt
> cmd.sendline('exit')
> cmd.close()
> 
> 
> - Jeff Younker - jeff at drinktomi.com -
> 



More information about the Tutor mailing list