For example - I have Test.exe file. Using python script, Opened CMD and did cd (moved to directory path) & Started Test.exe file, Now its was running until we did force exit, So Using UI we using this command (CTRL+c) to stop exit the running file But using python automation script how to exit it and Also Need to read the cmd data.
1 Answer
You can try
import os
os.system("taskkill /im test.exe")
or
os.system('wmic process where name="test.exe" delete')
2 Comments
Mofi
It would be good to post also the Python code using the subprocess module instead of the deprecated os.system. There can be get with os.environ the string value of environment variable
SystemRoot and use the string "C:\\Windows" on null returned (very unlikely) and concatenate the Windows directory path with the string "\\System32\\taskkill.exe" which is next executed with subprocess.run and so cmd.exe not used at all.Mofi
The
subprocess.run solution using wmic.exe requires the concatenation of the Windows directory path of environment variable SystemRoot with the string "\\System32\\wbem\\wmic.exe". I want to add that the usage of taskkill.exe with option /IM results in sending the WM_CLOSE message to all processes with name test.exe. So this solution is not good if multiple test.exe run often at the same time (cmd.exe, chrome.exe, firefox.exe, etc.).