Search code examples
pythonc++subprocess

How to run a c++ build file inside a python script?


Basically, I am trying to make some mesh models watertight using an algorithm. The algorithm is already built in C++ and can be run using the following command:

./manifold ../examples/input.obj ../examples/manifold.obj

Here I have to pass an input object, and the algorithm will output an object named manifold. I have thousands of input objects in different directories. And I am using a Python script to extract those input objects. The code is provided below.

def input_object(folder):
    for sub_folders in os.listdir(folder):
        for x in os.listdir(folder+sub_folders):
            if x == 'models':
                for y in os.listdir(folder+sub_folders+'/'+x):
                    if y == 'model_normalized.obj':
                        print(y)

                        #I want to execute the build file here


root = './02880940/'
input_object(root)

I want to execute the build file inside this commented area of the script. How can I do that? I am not well-versed in C++. Thanks in advance.


Solution

  • Have you ever heard of subprocess? if your C++ code is already built, simply import subprocess and call the run function. something like this:

    import subprocess
    def input_object(folder):
        for sub_folders in os.listdir(folder):
            for x in os.listdir(folder+sub_folders):
                if x == 'models':
                    for y in os.listdir(folder+sub_folders+'/'+x):
                        if y == 'model_normalized.obj':
                            print(y)
    
                            #I want to execute the build file here
    
                            subprocess.run("manifold.obj")
    
    root = './02880940/'
    count_messages(root)
    

    something like this