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.

asked Sep 23, 2019 at 7:58
2
  • This has nothing to do with python. Remove the python tag Commented Sep 23, 2019 at 8:03
  • The last example, where you use std::string will work if you pass the argument as path.c_str(). This returns the std::string's const char* representation. Commented Sep 23, 2019 at 12:13

3 Answers 3

2

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

system("py \"C:\\path\\python_script.py\"");
answered Sep 23, 2019 at 9:28
Sign up to request clarification or add additional context in comments.

Comments

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

answered Sep 23, 2019 at 8:49

Comments

0

Try string stream

#include <sstream>
std::stringstream ss;
ss << "py ";
ss << "\"C:\path\python_script.py\"";
system(ss.str().c_str());
answered Sep 23, 2019 at 8:05

1 Comment

stringstream is overkill for a problem that can simply be solved by character escaping...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.