calling programs from python

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

    calling programs from python

    Hello,

    I am trying to call a program in python as I would call it in the command
    line. For example I want to do the following from python: command > file.
    Then read this file. How do I call "command > file" ? Much help
    appreciated.

    -RR
  • David MacQuigg

    #2
    Re: calling programs from python

    On Mon, 01 Dec 2003 19:27:34 -0500, "RR" <ergodicsum@yah oo.com> wrote:
    [color=blue]
    >Hello,
    >
    >I am trying to call a program in python as I would call it in the command
    >line. For example I want to do the following from python: command > file.
    >Then read this file. How do I call "command > file" ? Much help
    >appreciated.[/color]

    Use os.system() For example, on Unix:[color=blue][color=green][color=darkred]
    >>> import os
    >>> os.system( 'command > file' )[/color][/color][/color]
    and on Windows:[color=blue][color=green][color=darkred]
    >>> os.system('star t C:\Python23\pyt hon.exe')[/color][/color][/color]

    -- Dave

    Comment

    • Kristofer Wouk

      #3
      Re: calling programs from python

      David MacQuigg wrote:[color=blue]
      > On Mon, 01 Dec 2003 19:27:34 -0500, "RR" <ergodicsum@yah oo.com> wrote:
      >
      >[color=green]
      >>Hello,
      >>
      >>I am trying to call a program in python as I would call it in the command
      >>line. For example I want to do the following from python: command > file.
      >>Then read this file. How do I call "command > file" ? Much help
      >>appreciated .[/color]
      >
      >
      > Use os.system() For example, on Unix:
      >[color=green][color=darkred]
      >>>>import os
      >>>>os.system ( 'command > file' )[/color][/color]
      >
      > and on Windows:
      >[color=green][color=darkred]
      >>>>os.system(' start C:\Python23\pyt hon.exe')[/color][/color]
      >
      >
      > -- Dave[/color]
      This will work too:

      import os
      os.popen('comma nd > file')

      Kris

      Comment

      • David MacQuigg

        #4
        Re: calling programs from python

        On Tue, 02 Dec 2003 16:02:26 GMT, Kristofer Wouk
        <kristofer@hotp op.com> wrote:
        [...][color=blue]
        >import os
        >os.popen('comm and > file')[/color]

        This is a little confusing. Do you intend the output from 'command'
        to go to 'file' or to the pipe you just opened?

        If you are opening a pipe (to be read as a file object) I would
        suggest something like this:
        [color=blue][color=green][color=darkred]
        >>> import os
        >>> file = os.popen('cat makefile | grep pyuic')
        >>> file.readlines( )[/color][/color][/color]
        ['\tpyuic ModelSelector.u i > ModelSelector.p y\n']

        If you are just executing a command and not needing to process any
        output from the command (other than its completion status), I would
        stick with the more general:
        [color=blue][color=green][color=darkred]
        >>> import os
        >>> status = os.system('comm and > file')
        >>> status
        >>> 0
        >>>[/color][/color][/color]

        --Dave

        Comment

        • Richard James

          #5
          Re: calling programs from python

          David MacQuigg <shuvit@127.0.0 .1> wrote in message news:<k04osvscn cmln06m83nam6jg hrpjg79jd7@4ax. com>...[color=blue]
          > On Mon, 01 Dec 2003 19:27:34 -0500, "RR" <ergodicsum@yah oo.com> wrote:
          >[color=green]
          > >Hello,
          > >
          > >I am trying to call a program in python as I would call it in the command
          > >line. For example I want to do the following from python: command > file.
          > >Then read this file. How do I call "command > file" ? Much help
          > >appreciated.[/color]
          >
          > Use os.system() For example, on Unix:[color=green][color=darkred]
          > >>> import os
          > >>> os.system( 'command > file' )[/color][/color]
          > and on Windows:[color=green][color=darkred]
          > >>> os.system('star t C:\Python23\pyt hon.exe')[/color][/color]
          >
          > -- Dave[/color]

          Windows Python os module has os.startfile()

          os.startfile('C :\Python23\pyth on.exe')

          -- Richard

          Comment

          Working...