How to close Command Prompt window after Batch file execution in python?

Deadly 提交于 2021-02-08 03:34:06

问题


I have a batch file which run a python based application which reads a messages continuously. I have python script which closes the application after a timeout period and Execute the Remaining code.

I am using Subprocess.Popen for Batch file run and terminate() call to terminate but the Cmd Window is still in OPen. It is not closing?

And the code is not executing untill the Window Closes. How can i forcly close the cmd?


回答1:


You can use psutil

And specifically the Process.terminate() function.

lets say your command prompt window name is "myscript"

for proc in psutil.process_iter():
    if proc.name() == "myscript":
        proc.terminate()

Or you can run another command, for instance taskkill

taskkill /F /T /IM myscript


来源:https://stackoverflow.com/questions/34284583/how-to-close-command-prompt-window-after-batch-file-execution-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!