FAQ
Hello,

I need to call an external command line .exe utility from my Python script.

What is the best way to capture the output (if any) and (optionally) direct it to my normal standard output?

There seem to be many options...

Thank you,
:)







____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Search Discussions

  • Alan Gauld at Mar 13, 2008 at 4:57 am
    "Allen Fowler" <allen.fowler at yahoo.com> wrote
    I need to call an external command line .exe utility from my Python
    script.

    What is the best way to capture the output (if any) and (optionally)
    direct it to my normal standard output?

    There seem to be many options...
    Historically there have been lots of methods introduced howeever
    the most recent and officially correct way is now tyo use the
    subprocess module. There are examples on the web pages
    to show what you want.

    Or read the Using the OS topic in my tutorial for the basics.

    HTH,

    --
    Alan Gauld
    Author of the Learn to Program web site
    Temorarily at:
    http://uk.geocities.com/alan.gauld at btinternet.com/
    Normally:
    http://www.freenetpages.co.uk/hp/alan.gauld
  • Kent Johnson at Mar 13, 2008 at 12:05 pm

    Allen Fowler wrote:
    Hello,

    I need to call an external command line .exe utility from my Python script.

    What is the best way to capture the output (if any) and (optionally) direct it to my normal standard output?
    subprocess.Popen().communicate() will do it:

    In [1]: import subprocess
    In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()
    In [10]: print x[0]
    ...

    If you just want stdout and stderr of the subprocess to go to stdout and
    stderr of the calling process you can omit those arguments to Popen().

    Kent
  • Linuxian iandsd at Mar 13, 2008 at 12:31 pm
    simplest way to run external commands !

    import os
    cmd="/usr/bin/ssh 10.0.0.20 uptime"
    os.popen(cmd)

    my cmd is just an example, use any cmd you want & its output will be
    displayed to you.
    hope this helps



    On Thu, Mar 13, 2008 at 12:05 PM, Kent Johnson wrote:

    Allen Fowler wrote:
    Hello,

    I need to call an external command line .exe utility from my Python script.
    What is the best way to capture the output (if any) and (optionally)
    direct it to my normal standard output?

    subprocess.Popen().communicate() will do it:

    In [1]: import subprocess
    In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()
    In [10]: print x[0]
    ...

    If you just want stdout and stderr of the subprocess to go to stdout and
    stderr of the calling process you can omit those arguments to Popen().

    Kent
    _______________________________________________
    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/20080313/ab8883e4/attachment.htm
  • Allen Fowler at Mar 13, 2008 at 6:39 pm
    Thank you for the help. :)

    ----- Original Message ----

    simplest way to run external commands !

    import os
    cmd="/usr/bin/ssh 10.0.0.20 uptime"
    os.popen(cmd)

    my cmd is just an example, use any cmd you want & its output will be displayed to you.
    hope this helps

    [SNIP]



    subprocess.Popen().communicate() will do it:

    In [1]: import subprocess
    In [7]: x=subprocess.Popen('ls', stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).communicate()
    In [10]: print x[0]
    ....

    If you just want stdout and stderr of the subprocess to go to stdout and
    stderr of the calling process you can omit those arguments to Popen().






    ____________________________________________________________________________________
    Looking for last minute shopping deals?
    Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/tutor/attachments/20080313/2865ebcf/attachment.htm
  • Luke Paireepinart at Mar 13, 2008 at 10:03 pm

    Allen Fowler wrote:
    Thank you for the help. :)

    ----- Original Message ----

    simplest way to run external commands !

    import os
    cmd="/usr/bin/ssh 10.0.0.20 <http://10.0.0.20> uptime"
    os.popen(cmd)
    This is deprecated in python 2.5+. Use subrpocess instead of os.popen
    to make sure your code continues to work in the future.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouptutor @
categoriespython
postedMar 13, '08 at 3:06a
activeMar 13, '08 at 10:03p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase