0

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.

3 Answers3

2

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

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

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

Fade
  • 85
  • 11
0

Try string stream

#include <sstream>

std::stringstream ss;
ss << "py ";
ss << "\"C:\path\python_script.py\"";
system(ss.str().c_str());
abhilb
  • 5,639
  • 2
  • 20
  • 26