popen2 usage

Steven Bethard steven.bethard at gmail.com
Tue Jul 19 22:47:03 EDT 2005


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



More information about the Python-list mailing list