Call Script From C++

I have a Python script that I've made to convert the output file of a C++ program. I'd like to have C++ call the script but can't seem to find a good way to do this.

I'm not trying to embed or extend Python code in C++. I just want to be able to call the Python script from within C++ and pass it arguments.

For example:
1
2
3
4
5
6
7
8
9
int main()
{
    string filename;
    doesSomethingCool();
    writesFile(filename);

    // Call the Python script passing a filename argument.
    niftyPythonScript(filenameToConvert);
}


Obviously this code won't work...it's just an example of what I'm hoping to do. Is there a way to call niftyPythonScript.py with the argument filenameToConvert?

What if the location of the script is in an arbitrary location (but still known). Suppose it's always in /vol/data/files/niftyPythonScript.py...
Last edited on
I think I've made progress on this. If I want to call a script from C++ I can do the following.

Using the example above:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    string filename;
    doesSomethingCool();
    writesFile(filename);

    // Call the Python script passing a filename argument.
    std::string command = "./niftyPythonScript.py ";
    command += filename;
    system(command.c_str());
}
closed account (Lv0f92yv)
Thanks to SteakRider from another post for this:
Edit: http://www.cplusplus.com/forum/windows/1341/ - SteakRider's post about windows system commands.

command += " file1.txt";

As if you were running from the terminal and executing your script from there, should work.

Then it will look like:

system( "./pythonscript file1.txt" );
Last edited on
As a follow up to this problem:

Suppose I know that the Python scripts are in the same directory as the C++ file.

Example:

Assume myCode and niftyPythonScript.py are both contained in /home/username/someDirectory/anotherDirectory/. I'd like the following two commands to work the same.

Command 1:
 
username@computer:~$ ./someDirectory/anotherDirectory/myCode


Command 2:
 
username@computer:~/someDirectory/anotherDirectory$ ./myCode


Where myCode is the executable from the C++ code that calls niftyPythonScript.py.

How can I construct command so that it will search the same relative path of the C++ file no matter where the user runs the C++ code from.
closed account (Lv0f92yv)
I think the ./scriptname is relative to the C++ executable that uses it. It should be relative by default, regardless of where you execute the C++ program from, as long as the C++ program is in the right place.

In short: Those two commands should do the same thing. If they don't then I am confused.
system command only works if your C++ program don't intend to receive any input based on the Python script output.

That is, Python script will output 1,2,3 and this is needed as input by C++ program. To enable that, you may want to consider fork (expensive), pipe, socket, msg etc API calls instead. (I assume you using Linux environment).
Topic archived. No new replies allowed.