python - 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.
解决方案
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')
推荐阅读
- xml - XSLT - 从具有相同属性名称的元素创建属性值数组
- angularjs - 从 AngularJS 调用时,Angular 路由器不更新浏览器 URL
- angular - Angular 6 - 过滤后单击表格行返回错误值
- flutter - 未处理的异常:错误状态:无法设置内容类型为“application/json”的请求的正文字段
- django - Django升级未加盐的MD5密码不匹配
- google-sheets - Arrayformula 适用于 countifs 但不适用于 sumifs?
- python - Conditional DAG Order based on Condition(带有If条件的For循环)
- bash - 给定别名,从 ssh config 获取 HostName
- javascript - 如何以编程方式比较来自两个不同数组的所有元素
- hive - Hive INSERT OVERWRITE 是否保证没有重复行?