5

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

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

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)

Is there any chance to do this?

philippos
  • 1,142
  • 5
  • 20
  • 41
  • 4
    Possible duplicate of [running c++ code from python](http://stackoverflow.com/questions/11113704/running-c-code-from-python) – Anil_M Oct 19 '16 at 15:09
  • Did you search SO for your topic? There are few posts that discusses executing C++ code in python. Here is one. e.g. http://stackoverflow.com/questions/11113704/running-c-code-from-python and – Anil_M Oct 19 '16 at 15:10
  • @Anil_M Thank you for the link. I did search for related questions but didn't find that one. I am reading it now and still can not feel that I found and answer to my question. – philippos Oct 19 '16 at 15:15
  • Is your C++ code in a main program or as a shared object/DLL ? – cdarke Oct 19 '16 at 15:22
  • @cdarke it is in a main program – philippos Oct 19 '16 at 15:26
  • OK, 2 more questions. How do you pass the filename to the main program - command-line `*argv[]` or `cin` or some other way? Which version of python are you using? The solution will be to use the `subprocess` module, if you are on 3.5, `subprocess.run()`. – cdarke Oct 19 '16 at 15:30
  • @cdarke I pass the filename by *argv[] and I am using python 2.7 – philippos Oct 19 '16 at 15:36
  • OK, I'll post a simple solution. – cdarke Oct 19 '16 at 15:45

1 Answers1

7

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

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

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Thanks a lot for the help. I have 2 questions as well if you don't mind. 1st, this is limited to windows I guess? (cause your wrote ./myprog) 2nd, I forgot to mention that my program takes, in addition to the filename, 4 another arguments and do some computation on them. So how could I include the arguments in this case? could it be something like proc = subprocess.Popen(["./myprog", filename], arg1, arg2,...)? – philippos Oct 19 '16 at 16:07
  • 4
    No, that's not Windows specific, which OS are you using? To add other arguments, add them to the list: `subprocess.Popen(["./myprog", filename, arg1, arg2],...)`. You might be able to get away with a string for the argument list instead, but usually a list is simpler. – cdarke Oct 19 '16 at 16:13
  • I just tried it on MAC and it worked with arguments and everything went fine! Thanks a lot! – philippos Oct 19 '16 at 16:28
  • Great! Next time you ask here bear in mind the questions I had to ask. Your mention of C++ made people think you wanted to call C++ from Python *in the same process*. You can do that using the Python API, although it is rather more complex than creating a child process. In this case the language was actually irrelevant. – cdarke Oct 19 '16 at 18:11