Source

fromsubprocessimportPopen,PIPE,STDOUTdefpipe(*commands,**kw):"""Run a chain of commands, returns the output of the last one. Arguments are a list of command line arguments, an optional keyword "stderr" can be used to capture stdandard error as well. Example: pipe(["dmesg"], ["grep", "hda"]) is like "dmesg | grep hda" """stderr=STDOUTifkw.get("stderr",False)elseNonepipe=Noneforcmdlineincommands:stdin=pipe.stdoutifpipeelseNonepipe=Popen(cmdline,stdin=stdin,stdout=PIPE,stderr=stderr)returnpipe.communicate()[0]