jcam77
jcam77

Reputation: 173

Exchanging info between running C++ code and python script

I have a python script that is called inside a C++ program. The python script creates a directory based on the current time, puts files in there, and then execution returns to C++. I want to save a parameters file into the directory created in the python program.

I figure my options are:

  1. Pass in the text to save in the parameters file to the Python program and have it create and save the file
  2. Return the location of the directory to C++ from python so it knows where to save the file
  3. Use C++ to locate the most recently created directory after execution of the python script and put file there

I'm not sure how to do any of this. My python script is not embedded. I use

    std::string test = "python analyzeData2.py";
system(test.c_str());

to call a python script.

Any ideas how to do this?

Upvotes: 0

Views: 59

Answers (1)

user590028
user590028

Reputation: 11730

I'd go with option B -- return the location of the directory to c++ from python so it knows where to save the file.

If you plan on using system(), something like this:

char* dirname[64];
FILE* fin;

system("python analyzeData2.py > created.log");
fin = fopen("created.log", "r");
fgets(dirname, sizeof(dirname), fin);
fclose(fin);
/* dirname has contents of created.log */
...

Upvotes: 1

Related Questions