os.spawn

cmkl cmkleffner at gmx.de
Wed Apr 30 12:10:34 CEST 2003


Jeroen Wolff <no at mail.com> wrote in message news:<Xns936C9D9E91B1Bthewolffman at 194.109.133.29>...
> Hi,
> 
> I want to run some shell commands from within python. 
> Wat i normal do at the shell prompt is:
> 'flow-cat <dirname> | flow-stat <param> > outputfile.txt'
> 
> I like to do this from within python and wait for it to finish and the 
> process the outputfile.txt
> Is this possible? The problem is with '|' and '>" piping....
> 
> Can somebody please help?
> 
> Thanks,
> 
> Jeroen Wolff

Hi,

I didn't tried this right now, but you may find Trent Mick's
process 0.5.0 interesting. See his talk at pycon:
http://starship.python.net/~tmick/downloads/process/pycon_lightning_talk.ppt
and his Website: http://starship.python.net/crew/tmick/

Basically process.py supports three different types of controlling
processes; Unix and Windows is supported as far as I understand;
Windows support needs Hammonds win32 module.

1) Process(): launch it and forget about the output (like os.system)
2) ProcessOpen(): interact with it (like os.popen)
3) ProcessOpenProxy(): eventlike interface to the launched process,
   needs Python with thread support (default on most platforms)

BTW: pexpect (on sourceforge) is another way to interact with external 
processes. pexpect does not need thread support but runs processes 
within pty's (pseodoterminal), so it is Unix only.

For your problem you can give it a try with ProcessOpen() 
without piping the output to a temporary file but use the 
stdout directly:

>>> import process
>>> CMD = 'flow-cat <dirname> | flow-stat <param>'
>>> p = process.ProcessOpen(cmd=CMD)
>>> p.stdout.read()
.......
>>> p.wait()   # returns the child's exit status

See Mick's presentation ...

Carl




More information about the Python-list mailing list