首页 > 解决方案 > Python opens executable, but it doesn't close on its own

问题描述

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" :

This is a photo with the .exe on the Task Manager :

And this is my Python code:

process = subprocess.run([r'C:\Users\Documents\kino\kino.exe'], shell = True)

If you still don't understand my problem, here is a video describing it.

标签: pythonexecutable

解决方案


Use pywin32 to get it done. Something like this will solve your issue

import win32com.client
 app = win32com.client.Dispatch("WScript.Shell")
 app.Run('Path/Yourexe.exe')

推荐阅读