Calling a C program from a Python Script

Brad Tilley bradtilley at gmail.com
Thu Dec 9 19:31:01 CET 2004


Grant Edwards wrote:
> Huh?  What do you mean "write a file open"?  You want to read a
> C source file and execute the C source?  If you have access to
> a C interpreter, I guess you could invoke the interpreter from
> python using popen, and feed the C source to it.  Alternatively
> you could invoke a compiler and linker from C to generate an
> executable and then execute the resulting file.
> 
> 
>>for root, files, dirs in os.walk(path)
>>     for f in files:
>>         try:
>>             EXECUTE_C_PROGRAM
> 
> 
> 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" 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

 From the Simpsons:
Frink: "Here we have an ordinary square."
Wiggum: "Whoa! Slow down egghead!"



More information about the Python-list mailing list