Running a command line program and reading the result as it runs

Gertjan Klein gklein at xs4all.nl
Fri Aug 23 08:03:43 EDT 2013


Peter Otten wrote:

> The following works on my linux system:
>
> import subprocess
>
> p = subprocess.Popen(
>      ["ping", "google.com"],
>      stdout=subprocess.PIPE)
>
> instream = iter(p.stdout.readline, "")
>
> for line in instream:
>      print line.rstrip()
>
> I don't have Windows available to test, but if it works there, too, the
> problem is the internal buffer used by Python's implementation of file
> iteration rather than the OS.

Excellent, that works on Windows as well. That conclusively proves that 
the buffering problem is in Python, not in the command that is executed. 
(Although that may happen, too, for some commends.)

Regards,
Gertjan.





More information about the Python-list mailing list