从Python脚本调用C程序 [英] Calling a C program from a Python Script

查看:99
本文介绍了从Python脚本调用C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以打开文件,然后用C读取程序然后

从这样的Python脚本中调用C程序:


os.walk(path)中的root,files,dirs
文件中的


尝试:

EXECUTE_C_PROGRAM


如果可能,这比纯Python解决方案快多少?


谢谢,


布拉德

Is it possible to write a file open, then read program in C and then
call the C program from a Python script like this:

for root, files, dirs in os.walk(path)
for f in files:
try:
EXECUTE_C_PROGRAM

If possible, how much faster would this be over a pure Python solution?

Thank you,

Brad

推荐答案

2004-12-09,Brad Tilley< br ******** @ gmail.com>写道:
On 2004-12-09, Brad Tilley <br********@gmail.com> wrote:
是否可以打开一个文件,然后在C中读取程序,然后从Python脚本中调用C程序,如下所示:


嗯?你的意思是写一个文件打开?你想阅读一个

C源文件并执行C源代码?如果您可以访问

一个C解释器,我猜你可以使用popen从

python调用解释器,并将C源提供给它。或者,你可以从C调用编译器和链接器生成一个

可执行文件,然后执行生成的文件。

for root,files,在os.walk(路径)dirs文件中的f:
尝试:
EXECUTE_C_PROGRAM


你将不得不解释清楚你是什​​么意思

" EXECUTE_C_PROGRAM"。如果你愿意,你当然可以运行一个从C源生成的
二进制可执行文件(例如,Linux下的一个/ b $ b ELF文件或者任何一个.exe文件。 Windows)。

如果可能的话,这比纯Python的解决方案要快多少?
Is it possible to write a file open, then read program in C and then
call the C program from a Python script like this:
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).
If possible, how much faster would this be over a pure Python
solution?




解决方案是什么?


-

格兰特爱德华兹格兰特哇!此外,我的保龄球平均价格为


visi.com无懈可击!!!



Solution to what?

--
Grant Edwards grante Yow! And furthermore,
at my bowling average is
visi.com unimpeachable!!!


Brad Tilley写道:
Brad Tilley wrote:
如果可能的话,这会比纯Python解决方案快多少?
If possible, how much faster would this be over a pure Python solution?




这就像蝙蝠侠的区别好的。


蝙蝠侠比以往更快



It is like the difference between Batman and Ever.

batman is faster than ever


Grant Edwards写道:
Grant Edwards wrote:
嗯?你的意思是写一个文件打开?你想阅读一个
C源​​文件并执行C源代码吗?如果你有权使用一个C解释器,我想你可以使用popen从
python调用解释器,并将C源提供给它。或者,你可以从C调用一个编译器和链接器来生成一个
可执行文件,然后执行生成的文件。

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.

用于root,文件,在os.walk(路径)dirs文件中的f:
尝试:
EXECUTE_C_PROGRAM
for root, files, dirs in os.walk(path)
for f in files:
try:
EXECUTE_C_PROGRAM



你将不得不解释清楚你的意思是什么?
EXECUTE_C_PROGRAM。如果你愿意,你当然可以运行一个从C源生成的二进制可执行文件(例如,Linux下的ELF文件或Windows下的.exe文件)。


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).




看起来我被指责了。我的意思是一个C程序打开并读取

文件而Python做其他一切。如何将C集成到这样的
a Python脚本中?


所以,而不是这样:


for os.walk(路径)中的root,files,dirs文件中f的


尝试:

x = file(f,''' rb'')

data = x.read()

x.close()

这个:


for root,files,dirs in os.walk(path)

for f in files:

try:

EXECUTE_C_PROGRAM


来自辛普森一家:

Frink:这里我们有一个普通的广场。

Wiggum:哇!减慢蛋头!



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!"


这篇关于从Python脚本调用C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆