Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if an application is open in Python?
A process is a program under execution. When an application runs on your operating system, it creates one or more processes. Python provides several methods to check if a specific application is currently running on your system.
We'll explore three different approaches to check if an application is open: using the psutil module, the subprocess module, and the wmi module for Windows systems.
Using psutil.process_iter() Function
The psutil module provides a cross-platform interface for retrieving information about running processes and system utilization. It works on Linux, Windows, macOS, Solaris, and AIX.
First, install psutil using ?
pip install psutil
Example Checking if Chrome is Running
Here's how to check if a specific process is currently running ?
import psutil
def check_if_process_running(process_name):
for process in psutil.process_iter(['name']):
if process.info['name'] == process_name:
return True
return False
# Check if Chrome is running
result = check_if_process_running("chrome.exe")
print(f"Is Chrome running? {result}")
Is Chrome running? False
Example Getting Detailed Process Information
You can also retrieve detailed information about all running processes ?
import psutil
# Get first 5 processes for demonstration
processes = list(psutil.process_iter())[:5]
for process in processes:
try:
print(f"Process: {process.name()} | PID: {process.pid}")
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
# System information
cpu_percent = psutil.cpu_percent()
memory_usage = psutil.virtual_memory()
print(f"\nCPU usage: {cpu_percent}%")
print(f"Memory usage: {memory_usage.percent}%")
Process: python.exe | PID: 1234 Process: notepad.exe | PID: 5678 Process: explorer.exe | PID: 9012 Process: svchost.exe | PID: 3456 Process: chrome.exe | PID: 7890 CPU usage: 10.6% Memory usage: 51.9%
Using subprocess Module
The subprocess module allows you to execute system commands from Python. On Windows, you can use the tasklist command to check running processes.
Example
This method uses Windows' built-in tasklist command ?
import subprocess
def is_process_running(process_name):
cmd = 'tasklist /fi "imagename eq {}"'.format(process_name)
output = subprocess.check_output(cmd, shell=True).decode()
if process_name.lower() in output.lower():
return True
else:
return False
result = is_process_running("notepad.exe")
print(f"Is Notepad running? {result}")
Is Notepad running? True
Using wmi Module (Windows Only)
Windows Management Instrumentation (WMI) is a Windows-specific tool for managing local and remote computers. The wmi module provides Python access to WMI functionality.
Install the wmi module ?
pip install wmi
Example
Here's how to use WMI to list running processes ?
import wmi
f = wmi.WMI()
print("PID Process Name")
print("-" * 25)
# Show first 10 processes for demonstration
count = 0
for process in f.Win32_Process():
if count < 10:
print(f"{process.ProcessId:<5} {process.Name}")
count += 1
else:
break
PID Process Name ------------------------- 0 System Idle Process 4 System 124 Registry 524 smss.exe 752 csrss.exe 868 csrss.exe 888 wininit.exe 940 services.exe 960 lsass.exe 320 winlogon.exe
Comparison
| Method | Platform | Advantages | Best For |
|---|---|---|---|
psutil |
Cross-platform | Rich process information, portable | Most applications |
subprocess |
OS-specific | Uses native OS commands | Simple checks |
wmi |
Windows only | Deep Windows integration | Windows-specific applications |
Conclusion
Use psutil for cross-platform process checking with detailed information. For Windows-only applications, subprocess with tasklist or wmi module provide native OS integration. The psutil approach is recommended for most use cases due to its portability and rich feature set.
