在python中执行批处理文件后如何关闭命令提示符窗口? [英] How to close Command Prompt window after Batch file execution in python?

查看:113
本文介绍了在python中执行批处理文件后如何关闭命令提示符窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,该文件运行一个基于python的应用程序,该应用程序连续读取消息.我有python脚本,该脚本会在超时后关闭应用程序并执行其余代码.

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.

我正在使用Subprocess.Popen来运行批处理文件并终止调用(终止),但Cmd窗口仍在OPen中.还没关门吗?

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?

直到窗口关闭,代码才执行.我如何强制关闭cmd?

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

推荐答案

您可以使用 psutil

特别是 Process.terminate()函数.

让我们说您的命令提示符窗口名称为"myscript"

lets say your command prompt window name is "myscript"

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

或者您可以运行其他命令,例如 taskkill

Or you can run another command, for instance taskkill

taskkill /F /T /IM myscript

这篇关于在python中执行批处理文件后如何关闭命令提示符窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆