Trying to run an external program

Discussion in 'Python' started by Brant Sears, Sep 20, 2006.

  1. Brant Sears

    Brant Sears Guest

    Hi. I'm new to Python and I am trying to use the latest release (2.5)
    on Windows XP.

    What I want to do is execute a program and have the results of the
    execution assigned to a variable. According to the documentation the
    way to do this is as follows:

    import commands
    x = commands.getstatusoutput('dir')

    This should assign "x" to be the output of the command "dir".
    However, when I run this (or any other command), x ends up being:

    (1, "'{' is not recognized as an internal or external command,
    \noperable program or batch file.")

    From looking through the documentation, I'm believing that the
    implementation of commands.getstatusoutput is actually some multi-
    step thing that starts with issuing the bracket character that is
    being choked on. This leads me to believe that Python or perhaps just
    the commands module is not setup correctly on my computer.

    I installed Python using the Python2-5.msi link that I found at:
    http://www.python.org/download/releases/2.5/

    I left everything the default during installation, so Python was
    installed to C:\Python25. The only other thing I did was add this
    PATH variable on my computer.

    Any ideas on what I might do to troubleshoot this?

    Thanks!

    Brant Sears
     
    Brant Sears, Sep 20, 2006
    #1
    1. Advertising

  2. Brant Sears

    Matimus Guest

    Brant Sears wrote:
    > Hi. I'm new to Python and I am trying to use the latest release (2.5)
    > on Windows XP.
    >
    > What I want to do is execute a program and have the results of the
    > execution assigned to a variable. According to the documentation the
    > way to do this is as follows:
    >
    > import commands
    > x = commands.getstatusoutput('dir')
    >
    > This should assign "x" to be the output of the command "dir".
    > However, when I run this (or any other command), x ends up being:
    >
    > (1, "'{' is not recognized as an internal or external command,
    > \noperable program or batch file.")
    >
    > From looking through the documentation, I'm believing that the
    > implementation of commands.getstatusoutput is actually some multi-
    > step thing that starts with issuing the bracket character that is
    > being choked on. This leads me to believe that Python or perhaps just
    > the commands module is not setup correctly on my computer.
    >
    > I installed Python using the Python2-5.msi link that I found at:
    > http://www.python.org/download/releases/2.5/
    >
    > I left everything the default during installation, so Python was
    > installed to C:\Python25. The only other thing I did was add this
    > PATH variable on my computer.
    >
    > Any ideas on what I might do to troubleshoot this?
    >
    > Thanks!
    >
    > Brant Sears


    commands is a Unix module, hence it will not work in Windows XP. If you
    don't need the return code try this:

    import os

    pipe = os.popen('dir')
    x = pipe.read()
    # do something with x

    -Matt
     
    Matimus, Sep 21, 2006
    #2
    1. Advertising

  3. Brant Sears wrote:

    > Any ideas on what I might do to troubleshoot this?


    As other stated out, you are using the wrong module. Try:

    >>> import os
    >>> p=os.popen('dir')
    >>> for l in p:

    .... print l
    ....
    --- OUTPUT OF DIR HERE ---
    >>> p.close()


    The return value of close is the return value of the command run. If you
    need to read stdout and stderr of your command or write to its stdin, use
    popen2, popen3, popen4 from the os-module.

    Regards
    Stephan
     
    Stephan Kuhagen, Sep 21, 2006
    #3
  4. Brant Sears

    Larry Bates Guest

    Brant Sears wrote:
    > Hi. I'm new to Python and I am trying to use the latest release (2.5) on
    > Windows XP.
    >
    > What I want to do is execute a program and have the results of the
    > execution assigned to a variable. According to the documentation the way
    > to do this is as follows:
    >
    > import commands
    > x = commands.getstatusoutput('dir')
    >
    > This should assign "x" to be the output of the command "dir". However,
    > when I run this (or any other command), x ends up being:
    >
    > (1, "'{' is not recognized as an internal or external command,\noperable
    > program or batch file.")
    >
    > From looking through the documentation, I'm believing that the
    > implementation of commands.getstatusoutput is actually some multi-step
    > thing that starts with issuing the bracket character that is being
    > choked on. This leads me to believe that Python or perhaps just the
    > commands module is not setup correctly on my computer.
    >
    > I installed Python using the Python2-5.msi link that I found at:
    > http://www.python.org/download/releases/2.5/
    >
    > I left everything the default during installation, so Python was
    > installed to C:\Python25. The only other thing I did was add this PATH
    > variable on my computer.
    >
    > Any ideas on what I might do to troubleshoot this?
    >
    > Thanks!
    >
    > Brant Sears


    Others have answered your specific question, but on a more general note:

    If you want directory information use os.listdir or glob.glob instead.
    No reason to shell out to do a dir. If you were using dir as an example,
    please disregard.

    -Larry Bates
     
    Larry Bates, Sep 21, 2006
    #4
  5. Brant Sears

    Larry Bates Guest

    Brant Sears wrote:
    > Hi. I'm new to Python and I am trying to use the latest release (2.5) on
    > Windows XP.
    >
    > What I want to do is execute a program and have the results of the
    > execution assigned to a variable. According to the documentation the way
    > to do this is as follows:
    >
    > import commands
    > x = commands.getstatusoutput('dir')
    >
    > This should assign "x" to be the output of the command "dir". However,
    > when I run this (or any other command), x ends up being:
    >
    > (1, "'{' is not recognized as an internal or external command,\noperable
    > program or batch file.")
    >
    > From looking through the documentation, I'm believing that the
    > implementation of commands.getstatusoutput is actually some multi-step
    > thing that starts with issuing the bracket character that is being
    > choked on. This leads me to believe that Python or perhaps just the
    > commands module is not setup correctly on my computer.
    >
    > I installed Python using the Python2-5.msi link that I found at:
    > http://www.python.org/download/releases/2.5/
    >
    > I left everything the default during installation, so Python was
    > installed to C:\Python25. The only other thing I did was add this PATH
    > variable on my computer.
    >
    > Any ideas on what I might do to troubleshoot this?
    >
    > Thanks!
    >
    > Brant Sears


    Others have answered your specific question, but on a more general note:

    If you want directory information use os.listdir or glob.glob instead.
    No reason to shell out to do a dir. If you were using dir as an example,
    please disregard.

    -Larry Bates
     
    Larry Bates, Sep 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitry Duginov

    Run external program during BUILD?

    Dmitry Duginov, May 2, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    718
    Chris R. Timmons
    May 2, 2005
  2. Tamer Higazi
    Replies:
    2
    Views:
    412
    Thomas M.
    Jul 22, 2003
  3. ash
    Replies:
    5
    Views:
    318
  4. Replies:
    5
    Views:
    12,488
  5. Thijs Janzen
    Replies:
    2
    Views:
    118
    Thijs Janzen
    Oct 19, 2008
Loading...

Share This Page