Calling a C program from a Python Script

Grant Edwards grante at visi.com
Thu Dec 9 14:10:57 EST 2004


On 2004-12-09, Brad Tilley <bradtilley at gmail.com> wrote:

>> You're going to have to explain clearly what you mean by
>> "EXECUTE_C_PROGRAM".  If you want to, you can certainly run a
>> binary executable that was generated from C source, (e.g. an
>> ELF file under Linux or whatever a .exe file is under
>> Windows).
>
> Appears I was finger-tied. I meant "a C program that opens and
> reads files"

That's still too vague to be meaningful.  Just reading a file
seems pointless:

 cat foo >/dev/null

Here, "cat" is a C program that "opens and reads" the file
named foo.
 
> while Python does everything else. How does one
> integrate C into a Python script like that?
>
> So, instead of this:
>
> for root, files, dirs in os.walk(path)
>       for f in files:
>           try:
>               x = file(f, 'rb')
>               data = x.read()
>               x.close()
> this:
>
> for root, files, dirs in os.walk(path)
>       for f in files:
>           try:
>               EXECUTE_C_PROGRAM

So you want the data returned to your Python program?  If so, 
you can't just execute a C program.  If you want to use the
data in the file, you have to read the data from _somewhere_.

You can read it directly from the file, or you can read it from
a pipe, where it was put by the program that read it from the
file.  The former is going to be far, far faster.

-- 
Grant Edwards                   grante             Yow!  Am I SHOPLIFTING?
                                  at               
                               visi.com            



More information about the Python-list mailing list