Python / Windows process control

Tim Golden mail at timgolden.me.uk
Thu Jul 10 11:39:39 EDT 2008


Salim Fadhley wrote:
> Does anybody know of a python module which can do process management
> on Windows? The sort of thing that we might usually do with
> taskmgr.exe or process explorer?
> 
> For example:
> 
> * Kill a process by ID
> * Find out which process ID is locking an object in the filesystem
> * Find out all the IDs of a particular .exe file
> * Find all the details of a currently running process (e.g. given an
> ID tell me which files it uses, niceness, runtime)

As far as I know, the closest you're going to come here is
WMI [1]. It won't do everything you ask, though. I don't know
how to find out which processes have a lock on a filesystem
object.

When you say "Find all the ids of a particular .exe file" I
assume you mean: all the processes which were started
by running that file.

<code>
import subprocess
import time

import wmi
c = wmi.WMI ()

#
# Kill a process by id
#
notepad = subprocess.Popen (["notepad.exe"])
time.sleep (1)
for process in c.Win32_Process (ProcessId=notepad.pid):
  process.Terminate ()

#
# Which process ids correspond to an .exe
#
for i in range (5): 
  subprocess.Popen (["notepad.exe"])

for process in c.Win32_Process (caption="notepad.exe"):
  print process.ProcessId

#
# _Some_ (but not all) of the information about each file
#
for process in c.Win32_Process (caption="notepad.exe"):
  print process
  process.Terminate ()

</code>

HTH

TJG

[1] http://timgolden.me.uk/python/wmi.html



More information about the Python-list mailing list