0

I have the following code:

import os
os.system("TASKKILL /F /IM notepad.exe")

I am aware that this command will forcefully kill the "notepad.exe" process. What I would like to know is, can I make that command more specific?

If I had both "helloworld.txt" and "goodbyeworld.txt" open, could I edit the code in any way to only kill "goodbyeworld.txt", instead of "notepad.exe" in general?

  • If you can find the PID of the process that has a given file open, then just use `/PID ` instead of `/IM `. – Blckknght Feb 20 '16 at 21:48
  • If you know the PID sure, otherwise no. The OS in general only knows that x instances of a program is open. It doesn't know what the program has open in itself. – Charlotte Dunois Feb 20 '16 at 21:48
  • How would I go about finding the PID of the process? –  Feb 20 '16 at 21:50
  • You can, but you'll probably have to use balck magic, like reading window title, parsing it and finding what file is open. – Lol4t0 Feb 20 '16 at 21:50
  • Does this help? http://stackoverflow.com/questions/550653/cross-platform-way-to-get-pids-by-process-name-in-python – idjaw Feb 20 '16 at 21:53
  • Yes, thank you! I managed to achieve what I set out to do. Although to find the PID I found the Command Prompt to work best. "tasklist" displays all processes and their IDs". –  Feb 20 '16 at 22:00

0 Answers0