Grokbase Groups Python tutor May 2009
FAQ
Hey all

I have a small program that when run from the command line, will
return a certain value for an arguement. Like this:
mfetchz 45
45j
so the program is mfetchz and the argument is 45

i know i can call the program with os.system("mfetchz 45")
but how do i get the return?

The OS is linux, if that matters

thanks

sk

Search Discussions

  • Vince spicer at May 29, 2009 at 9:36 pm
    import commands
    output = commands.getout("ls -lah")

    Vince
    On Fri, May 29, 2009 at 3:27 PM, shawn bright wrote:

    Hey all

    I have a small program that when run from the command line, will
    return a certain value for an arguement. Like this:
    mfetchz 45
    45j
    so the program is mfetchz and the argument is 45

    i know i can call the program with os.system("mfetchz 45")
    but how do i get the return?

    The OS is linux, if that matters

    thanks

    sk
    _______________________________________________
    Tutor maillist - Tutor at python.org
    http://mail.python.org/mailman/listinfo/tutor
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/tutor/attachments/20090529/83da618a/attachment.htm>
  • Vince spicer at May 29, 2009 at 9:37 pm
    Sorry *output

    import commands
    output = commands.getoutput("ls -lah")
    Vince

    On Fri, May 29, 2009 at 3:27 PM, shawn bright wrote:

    Hey all

    I have a small program that when run from the command line, will
    return a certain value for an arguement. Like this:
    mfetchz 45
    45j
    so the program is mfetchz and the argument is 45

    i know i can call the program with os.system("mfetchz 45")
    but how do i get the return?

    The OS is linux, if that matters

    thanks

    sk
    _______________________________________________
    Tutor maillist - Tutor at python.org
    http://mail.python.org/mailman/listinfo/tutor
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/tutor/attachments/20090529/3447f89e/attachment.htm>
  • Alan Gauld at May 29, 2009 at 10:09 pm
    "vince spicer" <vinces1979 at gmail.com> wrote
    import commands
    output = commands.getout("ls -lah")
    There are many ways to do this in Python including
    os.popen, commands and subprocess.

    But subprocess is the "officially correct" version, the others
    are deprecated and could theoretically disappear in a future
    version of Python. (But not soon I suspect because of the
    amount of legacy code that uses it!)

    But its definitely worth weaning oneself onto subprocess
    even though it is initially harder work. In the end the superior
    flexibility and consistency of approach pay back.

    Alan G.
  • Shawn bright at May 29, 2009 at 10:30 pm
    cool, thanks again
    sk
    On Fri, May 29, 2009 at 5:09 PM, Alan Gauld wrote:

    "vince spicer" <vinces1979 at gmail.com> wrote
    import commands
    output = commands.getout("ls -lah")
    There are many ways to do this in Python including os.popen, commands and
    subprocess.

    But subprocess is the "officially correct" version, the others are
    deprecated and could theoretically disappear in a future version of Python.
    (But not soon I suspect because of the amount of legacy code that uses it!)

    But its definitely worth weaning oneself onto subprocess even though it is
    initially harder work. In the end the superior flexibility and consistency
    of approach pay back.

    Alan G.


    _______________________________________________
    Tutor maillist ?- ?Tutor at python.org
    http://mail.python.org/mailman/listinfo/tutor
  • W W at May 29, 2009 at 9:39 pm

    On Fri, May 29, 2009 at 4:27 PM, shawn bright wrote:

    Hey all

    I have a small program that when run from the command line, will
    return a certain value for an arguement. Like this:
    mfetchz 45
    45j
    so the program is mfetchz and the argument is 45

    i know i can call the program with os.system("mfetchz 45")
    but how do i get the return?

    The OS is linux, if that matters
    use subprocess module:

    import subprocess

    op = subprocess.Popen(['mfetchz', '45'], stdout=subprocess.PIPE)

    for line in op.stdout:
    print line


    HTH,
    Wayne
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/tutor/attachments/20090529/9c0c39ad/attachment-0001.htm>
  • Shawn bright at May 29, 2009 at 9:49 pm
    kick butt, gents, thanks a lot
    sk
    On Fri, May 29, 2009 at 4:39 PM, W W wrote:
    On Fri, May 29, 2009 at 4:27 PM, shawn bright wrote:

    Hey all

    I have a small program that when run from the command line, will
    return a certain value for an arguement. Like this:
    mfetchz 45
    45j
    so the program is mfetchz and the argument is 45

    i know i can call the program with os.system("mfetchz 45")
    but how do i get the return?

    The OS is linux, if that matters
    use subprocess module:

    import subprocess

    op = subprocess.Popen(['mfetchz', '45'], stdout=subprocess.PIPE)

    for line in op.stdout:
    ??? print line


    HTH,
    Wayne

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouptutor @
categoriespython
postedMay 29, '09 at 9:27p
activeMay 29, '09 at 10:30p
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase