[Tutor] commands versus subprocess, I'm confused

Reed O'Brien reed at reedobrien.com
Fri Dec 26 16:02:56 CET 2008


On Dec 26, 2008, at 8:57, "Emad Nawfal (عماد نوفل)" <emadnawfal at g 
mail.com> wrote:

>
>
> 2008/12/26 Kent Johnson <kent37 at tds.net>
> On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (عماد نوفل)
> <emadnawfal at gmail.com> wrote:
> > suppose I have an external program that prints "testing the  
> subprocess
> > module"
> > I know I can run it through the commands module like this:
> >
> >>>> a = commands.getoutput("python3.0 hello.py")
> >>>> a
> > 'testing the subprocess module'
>
>
> > I cannot figure out how to do the same thing in the subprocess  
> module. Can
> > somebody please explain how to get the same behaviour from, say,
> > subprocess.call
>
> Sometthing like this, I think:
>
> proc = subprocess.Popen('python3.0 hello.py',
>                       shell=True,
>                       stdout=subprocess.PIPE,
>                       )
> stdout_value = proc.communicate()[0]
>
> (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html 
> )
>
> Kent
> Thank you Kent.
> It works, but isn't the commands module much simpler?  I don't know  
> why it's no more available in Python3.0

Subprocess was designed to replace all the os.popen classes since 2.4.

Commands is a wrapper for os.popen.

Aside from that commands AFAIK is unix only. Therefore it is less  
portable.

~ro 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081226/e67f0e7b/attachment.htm>


More information about the Tutor mailing list