问题
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