从python执行C ++代码 [英] Executing C++ code from python

查看:58
本文介绍了从python执行C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,我不知道这是否可行.

I am a beginner to python, and I have no idea if this seems to be a doable thing.

我在python中有一个简单的循环,可为我提供当前目录中的所有文件.我想做的是在python目录中的所有那些文件上执行我之前编写的C ++代码

I have a simple loop in python that gives me all the files in the current directory. What I want to do is to execute a C++ code I wrote before on all those files in the directory from python

建议的python循环应该是这样的

The proposed python loop should be something like this

import os
for filename in os.listdir(os.getcwd()):
    print filename
    (Execute the code.cpp on each file with each iteration)

有没有机会这样做?

推荐答案

通过Python轻松执行外部程序-不管使用哪种语言:

Fairly easy to execute an external program from Python - regardless of the language:

import os
import subprocess

for filename in os.listdir(os.getcwd()):   
    print filename
    proc = subprocess.Popen(["./myprog", filename])
    proc.wait()

用于参数的列表是特定于平台的,但是应该可以正常工作.您应该将"./myprog" 更改为您自己的程序(它不必位于当前目录中,它将使用PATH环境变量来找到它).

The list used for arguments is platform specific, but it should work OK. You should alter "./myprog" to your own program (it doesn't have to be in the current directory, it will use the PATH environment variable to find it).

这篇关于从python执行C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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