Determining running programs in Python
Asked Answered
W

4

20

How would I use Python to determine what programs are currently running. I am on Windows.

Weisbrodt answered 7/8, 2010 at 5:34 Comment(1)
Hey! Welcome to StackOverflow! It would be very helpful if you've posted things that you've tried but haven't worked for you. This lets people answering questions know what you're having problems with. Good Luck with your Question!Etiquette
T
26

Thanks to @hb2pencil for the WMIC command! Here's how you can pipe the output without a file:

import subprocess
cmd = 'WMIC PROCESS get Caption,Commandline,Processid'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print line
Tishtisha answered 27/7, 2011 at 13:15 Comment(0)
M
11
import os
os.system('WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid')
f = open("C:\ProcessList.txt")
plist = f.readlines()
f.close()

Now plist contains a formatted whitespace-separated list of processes:

  • The first column is the name of the executable that is running
  • The second column is the command that represents the running process
  • The third column is the process ID

This should be simple to parse with python. Note that the first row of data are labels for the columns, and not actual processes.

Note that this method only works on windows!

Motheaten answered 7/8, 2010 at 5:41 Comment(1)
Thanks, WMIC worked well. Unfortunately, the output text file wasn't so easy to parse. It's in UTF-16 so I had to decode and re-encode it as ASCII to easily parse using if (processName in procList): return trueFrescobaldi
F
8

Piping information from sub process commands is not ideal compared to an actual python tool meant for getting processes. Try the psutil module. To get a list of process numbers, do:

psutil.get_pid_list()

I'm afraid you have to download this module online, it is not included in python distributions, but this is a better way to solve your problem. To access the name of the process you have a number for, do:

psutil.Process(<number>).name

This should be what you are looking for. Also, here is a way to find if a specific process is running:

def process_exists(name):
    i = psutil.get_pid_list()
    for a in i:
        try:
            if str(psutil.Process(a).name) == name:
                return True
        except:
            pass
    return False

This uses the name of the executable file, so for example, to find a powershell window, you would do this:

process_exists("powershell.exe")
Fico answered 20/12, 2013 at 2:16 Comment(2)
get_pid_list() is deprecated, use pids() insteadBrana
I did some testing and for me on win10 psutil takes roughly 10x longer to perform the same task as piping WMICLyell
B
8

I was getting access denied with get_pid_list(). A newer method worked for me on windows and OSX:

import psutil

for proc in psutil.process_iter():
    try:
        if proc.name() == u"chrome.exe":
            print(proc)
            print proc.cmdline()
    except psutil.AccessDenied:
        print "Permission error or access denied on process"
Barefoot answered 6/6, 2015 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.