Reputation: 161
I'm trying to determine in python if a certain application is already open so that I don't open it twice. I've done a little research and have found that it is possible to pull the process name of a program, but the only issue I have with that is the program I am checking for itself has a pretty generic process name (in this case, "pythonw.exe" or "cmd.exe").
It does however have differing names in the application list of Windows Task Manager, so my question is if there is any way to use that to detect if a program is open or not. My workplace will not allow me to download additional programs or modules to use for this script, so the answer needs to use the os module or something similar that is already included in the windows library.
Upvotes: 5
Views: 7781
Reputation: 4739
I use the following code to check if a certain program is running:
import psutil #psutil - https://github.com/giampaolo/psutil
# Get a list of all running processes
list = psutil.pids()
# Go though list and check each processes executeable name for 'putty.exe'
for i in range(0, len(list)):
try:
p = psutil.Process(list[i])
if p.cmdline()[0].find("putty.exe") != -1:
# PuTTY found. Kill it
p.kill()
break;
except:
pass
PS: You can install your own modules using Virtual ENV or just choose an alternative installation path!
Upvotes: 9
Reputation: 609
I think the Python standard modules can't finish your requirement. It need some 3rd party libs, like win32api
, win32pdhutil
, win32con
.
Another approach is using windows command tasklist
. Use Python as the bat wrapper, and parse the output of tasklist
.
C:\Documents and Settings\Administrator>tasklist
Image Name PID Session Name Session# Mem Usage
========================= ====== ================ ======== ============
System Idle Process 0 Console 0 28 K
System 4 Console 0 236 K
smss.exe 812 Console 0 388 K
csrss.exe 860 Console 0 3,720 K
winlogon.exe 884 Console 0 4,148 K
services.exe 928 Console 0 3,356 K
lsass.exe 940 Console 0 5,904 K
vmacthlp.exe 1100 Console 0 2,348 K
...
Upvotes: 2