Starting a child process and getting its stdout?

Adonis Vargas adonis at REMOVETHISearthlink.net
Thu Dec 28 23:37:58 EST 2006


cypher543 wrote:
> This has been driving me insane for the last hour or so. I have search
> everywhere, and nothing works. I am trying to use the subprocess module
> to run a program and get its output line by line. But, it always waits
> for the process to terminate and then return the output all at once.
> 
> Can someone please show me some code that actually works for this sort
> of thing? It doesn't even have to use the subprocess module. Don't
> worry if the code isn't compatible with Windows. My program is targeted
> at Linux/UNIX users.
> 
> Thanks!
> 

try:

Python 2.5c1 (r25c1:51305, Aug 17 2006, 17:07:04)
[GCC 3.3.6] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import subprocess
 >>> cmd = "ls"
 >>> process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
 >>> print process.stdout.read()

For more info on how to do stdin and other things check out:
http://docs.python.org/lib/module-subprocess.html

Hope this helps.

Adonis



More information about the Python-list mailing list