Moving to subprocess from os.popen--pipe closes prematurely

Martin P. Hellwig martin.hellwig at dcuktec.org
Fri Dec 12 04:47:39 EST 2008


Kevin Walzer wrote:
> Hello,
> 
> I'm trying to move from os.popen to using subprocess, and I'm having 
> trouble with the pipe suddenly closing.
> 
> My old code looked something like this:
> 
<cut code and rest>

Hi Kevin,

You could try something more like:
 >>> import subprocess
 >>> cmd = subprocess.Popen([executable_path, executable_options], 
stdout=subprocess.PIPE, stdout=subprocess.PIPE)
 >>> std_out, std_err = cmd.communicate(standard_in_value)

std_out is a string though, you probably need to split it on newline to 
get the same sort of list and since it is buffered in memory you 
probably don't want to use .communicate if you expect megabytes of data 
back.

-- 
mph



More information about the Python-list mailing list