[Tutor] Calling C code in a python program

Michael P. Reilly arcege@shore.net
Thu, 27 Jul 2000 09:05:43 -0400 (EDT)

>   I have just started using python for science a week ago and need to
> know how to integrate a previously written program in C into my python
> interface...There must be a quicker method other than SWIG or the API
> methods that involves going into the C program and altering all definitions
> to include API definitions...please tell me there is! I do not need to
> implement separate parts of the C program individually all I need to do is
> call it, feed it its input and capture its output. 

For UNIX, you would probably want to use the popen2 module (in the
standard libary), which will allow you to send data to and read data
from a program.

>>> import popen2
>>> (child_stdout, child_stdin) = popen2.popen2('myprog arg1 arg2')
>>> child_stdin.write(data)
>>> whats_there = child_stdout.read()

For Windows, you will want to look at the win32pipe module in the
Windows extensions.  I'm not sure about Macs.


| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |