trouble with wrapping a c program

Chris Rebert clp2 at rebertia.com
Fri Jul 24 13:41:13 EDT 2009


On Fri, Jul 24, 2009 at 5:34 AM, Sanne Korzec<sanne at kortec.nl> wrote:
> Hi Mailing,
>
> I am using a c program, which first initializes for some seconds and then
> waits for user input (keyboard) to type something. When enter is pressed the
> c program continues.
<snip>
> Using the keyboard and then enter in the c program prompt works, but I wish
> to do this from the python script by sending the string from the python
> script.
>
> I am able to print the string from python with a print command or with a
> stdout.write command. But this simply prints it to the prompt, the c program
> does nothing with this printed string.
>
> Is there a way to pipe, stream, or send this string to the running c
> program?
<snip>
> Here is a small fragment of my code:
>
> #initialization
>
> cmd = [a list of my program and arguments]
>
> subprocess.Popen(cmd)   #starts the c program

import subprocess
cmd = [a list of my program and arguments]
process = subprocess.Popen(cmd, stdin=subprocess.PIPE)   #starts the c program
line = raw_input("Please enter a line of input for the C program:")
process.stdin.write(line)
process.stdin.write("\n")

You might want to study the docs for the subprocess module:
http://docs.python.org/library/subprocess.html

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list