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.

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

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

C (programming language), C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone  C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language.

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

C Programming Tutorial for Beginners, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system​  The C programming language is a computer programming language that was developed to do system programming for the operating system UNIX and is an imperative programming language. C was developed in the early 1970s by Ken Thompson and Dennis Ritchie at Bell Labs. It is a procedural language, which means that people can write their programs as a

Try string stream

#include <sstream>

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

"C" Programming Language: Brian Kernighan, C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available

Learn C, The best site for C and C++ programming. Popular, beginner-friendly C and C++ tutorials to help you become an expert! C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for

C Tutorial, C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of  Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.

C Language - Overview, C++ is nearly a superset of C language (There are few programs that may compile in C, but not in C++). Beginning with C programming: Structure of a C program Hello World using C++. Just to give you a little excitement about C++ programming, I'm going to give you a small conventional C++ Hello World program, You can try it using Demo link. C++ is a super set of C programming with additional implementation of object-oriented concepts.

Comments
  • This has nothing to do with python. Remove the python tag
  • 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.
  • stringstream is overkill for a problem that can simply be solved by character escaping...