Run program from within Python

Discussion in 'Python' started by frankrentef, Aug 6, 2008.

  1. frankrentef

    frankrentef Guest

    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\OpenProgram.exe 1 1')
    keygen = stdout.read()
    print "The keygen value is: %s" % keygen


    from the command line if I execute "OpenProgram.exe 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..
    frankrentef, Aug 6, 2008
    #1
    1. Advertising

  2. On Aug 6, 2:42 pm, frankrentef <> wrote:
    > 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\OpenProgram.exe 1 1')
    > keygen = stdout.read()
    > print "The keygen value is: %s" % keygen
    >
    > from the command line if I execute "OpenProgram.exe 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:

    http://blog.doughellmann.com/2007/07/pymotw-subprocess.html
    http://docs.python.org/lib/module-subprocess.html

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

    Mike
    Mike Driscoll, Aug 6, 2008
    #2
    1. Advertising

  3. frankrentef

    Guest

    On Aug 6, 3:42 pm, frankrentef <> wrote:
    > stdout, stdin = popen2.popen2('c:\test\OpenProgram.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\\OpenProgram.exe
    1 1') or use a raw string (r'c:\test\OpenProgram.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
    , Aug 6, 2008
    #3
  4. frankrentef

    frankrentef Guest

    THNX for the links... lotta reading for the newbie!
    frankrentef, Aug 6, 2008
    #4
    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. pradeep nair
    Replies:
    2
    Views:
    534
  2. jmDesktop
    Replies:
    15
    Views:
    464
  3. Robinson
    Replies:
    2
    Views:
    2,161
  4. Robinson
    Replies:
    0
    Views:
    364
    Robinson
    Nov 1, 2009
  5. Tommo
    Replies:
    3
    Views:
    171
Loading...

Share This Page