<p dir="ltr">On May 24, 2013 7:06 AM, "Luca Cerone" <<a href="mailto:luca.cerone@gmail.com">luca.cerone@gmail.com</a>> wrote:<br>
><br>
> Hi everybody,<br>
> I am new to the group (and relatively new to Python)<br>
> so I am sorry if this issues has been discussed (although searching for topics in the group I couldn't find a solution to my problem).<br>
><br>
> I am using Python 2.7.3 to analyse the output of two 3rd parties programs that can be launched in a linux shell as:<br>
><br>
> program1 | program2<br>
><br>
> To do this I have written a function that pipes program1 and program2 (using subprocess.Popen) and the stdout of the subprocess, and a function that parses the output:<br>
><br>
> A basic example:<br>
><br>
> from subprocess import Popen, STDOUT, PIPE<br>
> def run():<br>
> p1 = Popen(['program1'], stdout = PIPE, stderr = STDOUT)<br>
> p2 = Popen(['program2'], stdin = p1.stdout, stdout = PIPE, stderr = STDOUT)</p>
<p dir="ltr">Could you provide the *actual* commands you're using, rather than the generic "program1" and "program2" placeholders? It's *very* common for people to get the tokenization of a command line wrong (see the Note box in <a href="http://docs.python.org/2/library/subprocess.html#subprocess.Popen">http://docs.python.org/2/library/subprocess.html#subprocess.Popen</a> for some relevant advice).</p>
<p dir="ltr">> p1.stdout.close()<br>
> return p2.stdout<br>
><br>
><br>
> def parse(out):<br>
> for row in out:<br>
> print row<br>
> #do something else with each line<br>
> out.close()<br>
> return parsed_output<br>
><br>
><br>
> # main block here<br>
><br>
> pout = run()<br>
><br>
> parsed = parse(pout)<br>
><br>
> #--- END OF PROGRAM ----#<br>
><br>
> I want to parse the output of 'program1 | program2' line by line because the output is very large.<br>
><br>
> When running the code above, occasionally some error occurs (IOERROR: [Errno 0]).</p>
<p dir="ltr">Could you provide the full & complete error message and exception traceback?</p>
<p dir="ltr">> However this error doesn't occur if I code the run() function as:<br>
><br>
> def run():<br>
> p = Popen('program1 | program2', shell = True, stderr = STDOUT, stdout = PIPE)<br>
> return p.stdout<br>
><br>
> I really can't understand why the first version causes errors, while the second one doesn't.<br>
><br>
> Can you please help me understanding what's the difference between the two cases?</p>
<p dir="ltr">One obvious difference between the 2 approaches is that the shell doesn't redirect the stderr streams of the programs, whereas you /are/ redirecting the stderrs to stdout in the non-shell version of your code. But this is unlikely to be causing the error you're currently seeing.</p>
<p dir="ltr">You may also want to provide /dev/null as p1's stdin, out of an abundance of caution.</p>
<p dir="ltr">Lastly, you may want to consider using a wrapper library such as <a href="http://plumbum.readthedocs.org/en/latest/">http://plumbum.readthedocs.org/en/latest/</a> , which makes it easier to do pipelining and other such "fancy" things with subprocesses, while still avoiding the many perils of the shell.</p>
<p dir="ltr">Cheers,<br>
Chris<br>
--<br>
Be patient; it's Memorial Day weekend.</p>