Run program from within Python

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

    Run program from within Python

    Greetings all...

    Newbie to Python... need help with opening a file from within
    Python... see the following code.


    import popen2
    stdout, stdin = popen2.popen2(' c:\test\OpenPro gram.exe 1 1')
    keygen = stdout.read()
    print "The keygen value is: %s" % keygen


    from the command line if I execute "OpenProgram.ex e 1 1" a number is
    returned. ("1 1" are required to return the value needed.) Ultimately
    I want to take that number and apply it to another script, but the
    program is not running.

    Suggestions?


    NEWBIE to Python..
  • Mike Driscoll

    #2
    Re: Run program from within Python

    On Aug 6, 2:42 pm, frankrentef <frankren...@ya hoo.comwrote:
    Greetings all...
    >
    Newbie to Python... need help with opening a file from within
    Python... see the following code.
    >
    import popen2
    stdout, stdin = popen2.popen2(' c:\test\OpenPro gram.exe 1 1')
    keygen = stdout.read()
    print "The keygen value is: %s" % keygen
    >
    from the command line if I execute "OpenProgram.ex e 1 1" a number is
    returned.  ("1 1" are required to return the value needed.) Ultimately
    I want to take that number and apply it to another script, but the
    program is not running.
    >
    Suggestions?
    >
    NEWBIE to Python..
    If you're using Python 2.4+, popen2 is deprecated. I recommend reading
    up on the subprocess module instead. Here's a couple links:


    Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...


    The first one also explains how to communicate with a process you
    opened.

    Mike

    Comment

    • giltay@gmail.com

      #3
      Re: Run program from within Python

      On Aug 6, 3:42 pm, frankrentef <frankren...@ya hoo.comwrote:
      stdout, stdin = popen2.popen2(' c:\test\OpenPro gram.exe 1 1')
      What Mike said about subprocess.

      Also, in regular Python strings, \t means a tab character. You need
      to replace \ with \\ in the programme path ('c:\\test\\Ope nProgram.exe
      1 1') or use a raw string (r'c:\test\Open Program.exe 1 1'). (The r
      informs the Python parser that backslashes are to be used veratim, not
      as special code. If you're using Windows, raw strings make for fewer
      headaches when dealing with file paths.)

      Geoff G-T

      Comment

      • frankrentef

        #4
        Re: Run program from within Python

        THNX for the links... lotta reading for the newbie!


        Comment

        Working...