popen2 usage

Discussion in 'Python' started by jb, Jul 19, 2005.

  1. jb

    jb Guest

    Hi there:

    I need help with popen2 usage. I am coding on Windows 2000 environment
    and I am basically trying to run command line executable program that
    accepts command line arguments from user. I want to be able to provide
    these arguments through input pipe so that executable does not require
    any intervention from the user. The way I am doing this is as below:

    out1, in1 = popen2.popen2("testme.exe > abc.txt")
    in1.write('-test1')
    in1.flush()
    in1.close()

    But this does not seem to be working, when I open abc.txt file it does
    not show '-test1' argument that was supplied via in1.write method. This
    causing executable to wait forever unless user manually kills the
    process.

    Please help

    Regards,
    jb
    jb, Jul 19, 2005
    #1
    1. Advertising

  2. jb wrote:
    > Hi there:
    >
    > I need help with popen2 usage. I am coding on Windows 2000 environment
    > and I am basically trying to run command line executable program that
    > accepts command line arguments from user. I want to be able to provide
    > these arguments through input pipe so that executable does not require
    > any intervention from the user. The way I am doing this is as below:
    >
    > out1, in1 = popen2.popen2("testme.exe > abc.txt")
    > in1.write('-test1')
    > in1.flush()
    > in1.close()
    >
    > But this does not seem to be working, when I open abc.txt file it does
    > not show '-test1' argument that was supplied via in1.write method. This
    > causing executable to wait forever unless user manually kills the
    > process.


    I'm confused; is "-test1" a command line argument to testme.exe? Or is
    it the text that testme.exe should receive from standard input?

    Either way, I would suggest using subprocess instead of popen*.

    To pass -test1 as a command line argument, do something like:

    import subprocess as sp
    p = sp.Popen(["testme.exe", "-test1"], stdout=sp.PIPE)
    out1 = sp.stdout.read()

    To pass -test1 through standard input, do something like:

    import subprocess as sp
    p = sp.Popen(["testme.exe"], stdout=sp.PIPE)
    p.stdin.write("-test1")
    p.stdin.flush()
    p.stdin.close()
    out1 = p.stdout

    HTH,

    STeVe
    Steven Bethard, Jul 20, 2005
    #2
    1. Advertising

  3. jb

    jb Guest

    Actually, "-test1" is a text argument that testme.exe should receive
    from standard input. For example,
    Executing testme.exe generates the following output,
    Please select one of the following options:
    1) test1
    2) test2
    3) exit
    Please enter your option here:-test1 <-This -test1 is what user would
    type

    Thanks,
    -JB


    Steven Bethard wrote:
    > jb wrote:
    > > Hi there:
    > >
    > > I need help with popen2 usage. I am coding on Windows 2000 environment
    > > and I am basically trying to run command line executable program that
    > > accepts command line arguments from user. I want to be able to provide
    > > these arguments through input pipe so that executable does not require
    > > any intervention from the user. The way I am doing this is as below:
    > >
    > > out1, in1 = popen2.popen2("testme.exe > abc.txt")
    > > in1.write('-test1')
    > > in1.flush()
    > > in1.close()
    > >
    > > But this does not seem to be working, when I open abc.txt file it does
    > > not show '-test1' argument that was supplied via in1.write method. This
    > > causing executable to wait forever unless user manually kills the
    > > process.

    >
    > I'm confused; is "-test1" a command line argument to testme.exe? Or is
    > it the text that testme.exe should receive from standard input?
    >
    > Either way, I would suggest using subprocess instead of popen*.
    >
    > To pass -test1 as a command line argument, do something like:
    >
    > import subprocess as sp
    > p = sp.Popen(["testme.exe", "-test1"], stdout=sp.PIPE)
    > out1 = sp.stdout.read()
    >
    > To pass -test1 through standard input, do something like:
    >
    > import subprocess as sp
    > p = sp.Popen(["testme.exe"], stdout=sp.PIPE)
    > p.stdin.write("-test1")
    > p.stdin.flush()
    > p.stdin.close()
    > out1 = p.stdout
    >
    > HTH,
    >
    > STeVe
    jb, Jul 21, 2005
    #3
  4. On 21 Jul 2005 06:10:03 -0700, "jb" <> declaimed the
    following in comp.lang.python:

    > Actually, "-test1" is a text argument that testme.exe should receive
    > from standard input. For example,
    > Executing testme.exe generates the following output,
    > Please select one of the following options:
    > 1) test1
    > 2) test2
    > 3) exit
    > Please enter your option here:-test1 <-This -test1 is what user would
    > type
    >

    Why would the user type the hyphen? Your prompt seems to imply
    that the user should type JUST "1", "2", or "3" (or, if the numbers are
    meaningless, "test1", "test2", "exit" with no "-").

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Jul 21, 2005
    #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. Guy

    popen2

    Guy, Aug 12, 2003, in forum: Python
    Replies:
    1
    Views:
    2,842
    Donn Cave
    Aug 12, 2003
  2. cherico

    popen2 with large input

    cherico, Jan 29, 2004, in forum: Python
    Replies:
    2
    Views:
    322
    Jeff Epler
    Jan 29, 2004
  3. Diez B. Roggisch

    popen2 trouble

    Diez B. Roggisch, Apr 2, 2004, in forum: Python
    Replies:
    2
    Views:
    315
    Diez B. Roggisch
    Apr 5, 2004
  4. A. Lloyd Flanagan

    Possible problem with popen2 module

    A. Lloyd Flanagan, Apr 30, 2004, in forum: Python
    Replies:
    2
    Views:
    313
    A. Lloyd Flanagan
    May 3, 2004
  5. Guest
    Replies:
    1
    Views:
    477
    Donn Cave
    Jun 15, 2004
Loading...

Share This Page