Little background: Code::Blocks is an IDE with a C++ integrated compiler. When creating a C++ project, it creates a .exe file so you can run the project.
So now I want to run that executable file using a Python script (Using VSCode). I tried subprocess.call(), subprocess.run() and subprocess.Popen(), and all of them start the background process, but it doesn't compile, so it just keeps running on the Task Manager. If I run it manually (by double-clicking it) then it opens, it closes and I get my correct answer on the output file.
This is the C++ project folder for the problem "kino" : enter image description here
This is a photo with the .exe on the Task Manager : enter image description here
And this is my Python code:
process = subprocess.run([r'C:\Users\Documents\kino\kino.exe'], shell = True)
I want to say that I also tried subprocess.kill(), but it should terminate on its own (and I don't get my answer).
-
What happens when you run subprocess.call([r'C:\Users\Documents\kino\kino.exe']) ? I think it would be the right choiceroygbiv– roygbiv2020年06月10日 20:10:05 +00:00Commented Jun 10, 2020 at 20:10
-
What does kino.exe do? Could it be waiting for input on stdin? Maybe it works differently depending on if its stdin is connected to a console or not?Miles Budnek– Miles Budnek2020年06月10日 20:20:02 +00:00Commented Jun 10, 2020 at 20:20
-
The same thing happens when I run subprocess.call(), but the program freezes until I close the executable.Matteo.Verz– Matteo.Verz2020年06月11日 06:21:22 +00:00Commented Jun 11, 2020 at 6:21
-
I have a .in file (for input), a .out file (for output), the main.cpp file (which contains the c++ code) and the executable. The executable takes the input from the .in file, runs it using the .cpp file and prints the answer on the .out file. Should I post a small clip of how it works?Matteo.Verz– Matteo.Verz2020年06月11日 06:22:43 +00:00Commented Jun 11, 2020 at 6:22
2 Answers 2
Or you can just do it with subprocess
import subprocess
subprocess.call(["C:\\Users\\Documents\\kino\\kino.exe"])
Comments
Instead of running a subprocess you could execute the program with msdos commands:
import os
os.system('C:\Users\Documents\kino\kino.exe')
The only problem is that this will block your python script until the .exe program stops running.