How to catch a line with Popen

Nobody nobody at nowhere.com
Sat May 28 18:15:06 EDT 2011


On Sun, 29 May 2011 00:01:56 +0800, TheSaint wrote:

> I'm looking into subprocess.Popen docs. I've launch the program with its
> arguments and that's smooth. I'm expecting to read the output by
> *comunicate()* at every line that prgram may blow during the process, but
> the output is given only when the child process is ended.

.communicate() reads until EOF, .wait()s for the process to terminate,
then returns the output as a pair of strings.

If you want to read data while the process is running, use the process'
.stdout and/or .stderr members, e.g.:

	p = subprocess.Popen(...)
	for line in p.stdout:
	    ...
	p.wait()

Don't forget to .wait() on the process when you're done, regardless of
whether you actually need the exit code.




More information about the Python-list mailing list