Python Forum
How to write a script to execute a program need passing additional input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a script to execute a program need passing additional input?
#1
I would like the python test.py to run a program after it been compiled to a.out and to read test.dat file:

import subprocess
import os

cmd='./a.out'
subprocess.call(cmd, shell=True)
cmd='test.data'
subprocess.call(cmd, shell=True)
the c++ program is in the test.cpp file:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string filename("");
    cout<<"Please enter the file name:"<<endl;
    cin>>filename;
    cout<<"File name is: "<<filename<<endl;

    return 0;
}
execute python test.py, and it stuck there, and looks like the program are waiting for me to input 'test.data', the second subprocess.call seems not working.

How can I do this? Thanks.

So no one ever encountered this problem?
Reply
#2
You can restructure your c++ code to process command line arguments, e.g.

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv) {
    string filename(""); // do something with argv[1]
    cout<<"Please enter the file name:"<<endl;
    cin>>filename; // argv[1]
    cout<<"File name is: "<<filename<<endl;
 
    return 0;
}
and run as usual:
os.call(["./a.out", "filename"])
Reply
#3
Thank you very much, scidam!

While I am actually looking for a solution of changing on the Python side. The C++ code is mature and I prefer to leave it there.

Best!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can a program execute code in iPython shell and get result? deanhystad 3 1,662 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  Is this possible to write a script for checking data from website? WanW 2 1,081 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  Is it possible to write a python script to block twitter feeds? cubangt 0 837 Apr-07-2022, 04:14 PM
Last Post: cubangt
  Issue with program not passing to other elif condition Abdirahman 6 2,060 Nov-21-2021, 07:04 AM
Last Post: Abdirahman
  execute python script guy7200 1 1,574 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt
  write a program which prints the day in english ben1122 10 3,912 Jul-25-2021, 05:55 PM
Last Post: ben1122
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,234 May-18-2021, 06:31 AM
Last Post: Skaperen
  Passing flags to python script, through a function xbit 4 3,874 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Unable to execute input(). jahuja73 5 4,372 Feb-18-2021, 07:41 AM
Last Post: bowlofred
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,011 Nov-13-2020, 07:43 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020