user10615957
user10615957

Reputation:

C++ : Trigger a python script stored on my machine, from inside a C++ script

I would like to trigger a python script from my C++ script. The python script is independent, I do not need to pass it anything from C++, I also do not need anything returned to C++.

I would also like to pause execution of the C++ script until the python script has finished.

I have tried the embedding solutions and the wrapping solutions offered online, but I am looking for something much simpler.

I have attempted the following.

include cstdlib
system("py "C:\path\python_script.py"");

This attempt has problems with the double quotation mark syntax. I then attempted this to deal with the double quotation mark probem.

include cstdlib
system("py " + char(34) + "C:\path\python_script.py" + char(34));

I then received the error "expression must have integral or unscoped enum type". It seems as though you can't concatenate strings this way in C++? For my final attempt, I tried to concatenate the string in pieces.

include cstdlib
string   path1 = "py ";
string   path2 = "C:\path\python_script.py";
string   path = python_path1 + char(34) + python_path2 + char(34);
system(path);

I now receive the error "no suitable conversion function from "std::string" to "const char" exists". Any help would be greatly appreciated.

Upvotes: 0

Views: 122

Answers (3)

user1531591
user1531591

Reputation:

As other answer tell you add \ to escape the " and also double escape your \ path separator :

system("py \"C:\\path\\python_script.py\"");

Upvotes: 2

Fade
Fade

Reputation: 85

You can try system("py \"C:\path\python_script.py\"");.

This way you escape the quotation mark and can write it into a string.

Have a look at this post

Upvotes: 1

abhilb
abhilb

Reputation: 5757

Try string stream

#include <sstream>

std::stringstream ss;
ss << "py ";
ss << "\"C:\path\python_script.py\"";
system(ss.str().c_str());

Upvotes: 0

Related Questions