[Tutor] Having a return when subprocess.Popen finishes

Nick Raptis airscorp at otenet.gr
Thu Jul 8 15:33:02 CEST 2010


subprocess.Popen is a class, and as such it returns an object which can 
do a lot of stuff besides just reading the output.

What you want to do here is using it's communicate() method as such:

output, errors = ping.communicate()


Also, there is a quicker way, I think from version 2.7 forward: use  the 
shortcut
output = subprocess.check_output("your command here")

Always check latest documentation for your python version too
http://docs.python.org/library/subprocess.html

Nick

On 07/08/2010 04:04 PM, Paul VanGundy wrote:
> Hi All,
>
> I'm trying to get data from subprocess.Popen. To be specific, I am
> trying to read a ping echo and take the output and assign it to a
> variable like below:
>
> ping = subprocess.Popen("ping -c 5 %s" % (server),
> stdout=subprocess.PIPE, shell=True)
>
> However, when I run the command the output that gets assigned to my ping
> variable is something along the lines of '<subprocess.Popen object at
> 0x9524bec>' and am not returned to>>>  prompt. I know that is the proper
> output but I need to be able to capture the ping replies and assign
> those to a variable. I tried adding a \r and \n at the end of my cmd.
> Any help would be greatly appreciated. I'm open to improvements,
> different ways of doing it and corrections. :) If more info is needed
> let me know. Thanks.
>
> /paul
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>    


More information about the Tutor mailing list