Chaining programs with pipe

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Aug 22 04:56:18 CEST 2007


On 21 ago, 21:40, Karthik Gurusamy <kar1... at gmail.com> wrote:
> On Aug 21, 3:09 pm, avishay <avish... at yahoo.com> wrote:
>
> > I'm trying to chain two programs with a pipe (the output of one
> > feeding the input of the other). I managed to capture the output and
> > feeding the input of each program independently with popen, but how do
> > I tie them together? Is there a solution that works equally on all
> > platforms?
>
> Not sure on non-unix platforms, but in unix like platforms it's best
> to reuse shell's power.
>
> >>> import commands
> >>> commands.getoutput('ls | wc')

Using the shell is OK but I think the subprocess module is more
portable. This example is on Windows:

import subprocess
subprocess.call("dir /b | sort", shell=True)

~DFDADD.tmp
~DFF71E.tmp
1.bak
1.cmd
1.csv
1.py
1.txt
2.py
2A0B17.dmp [...]

--
Gabriel Genellina




More information about the Python-list mailing list