Trying to run an external program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brant Sears

    #1

    Trying to run an external program

    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.getsta tusoutput('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.getsta tusoutput 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:


    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
  • Matimus

    #2
    Re: Trying to run an external program

    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.getsta tusoutput('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.getsta tusoutput 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:

    >
    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

    Comment

    • Stephan Kuhagen

      #3
      Re: Trying to run an external program

      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('d ir')
      >>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

      Comment

      • Larry Bates

        #4
        Re: Trying to run an external program

        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.getsta tusoutput('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,\nopera ble
        program or batch file.")
        >
        From looking through the documentation, I'm believing that the
        implementation of commands.getsta tusoutput 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:

        >
        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

        Comment

        • Larry Bates

          #5
          Re: Trying to run an external program

          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.getsta tusoutput('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,\nopera ble
          program or batch file.")
          >
          From looking through the documentation, I'm believing that the
          implementation of commands.getsta tusoutput 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:

          >
          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


          Comment

          Working...