Open In App

How to check if an application is open in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

This article is about How to check if an application is open in a system using Python. You can also refer to the article Python – Get list of running processes for further information.

In the below approaches, we will be checking if chrome.exe is open in our system or not.

Using psutil

The psutil is a system monitoring and system utilization module of python. It is useful mainly for system monitoring, profiling and limiting process resources, and management of running processes. Usage of resources like CPU, memory, disks, network, sensors can be monitored. It is supported in Python versions 2.6, 2.7, and 3.4+. You can install psutil module by using the following command

pip install psutil

We will use the psutil.process_iter() method, it returns an iterator yielding a process class instance for all running processes on the local machine.

Python3




# import module
import psutil
  
# check if chrome is open
"chrome.exe" in (i.name() for i in psutil.process_iter())


Output:

True

We import the psutil module. Then we search for chrome.exe in all running processes on the local machine using psutil.process_iter(). If found it will return output as TRUE, else FALSE.

Using WMI (only Windows User) 

The wmi module can be used to gain system information of a Windows machine and can be installed using the below command: 

pip install wmi

Its working is similar to psutil. Here, we check if a particular process name is present in the list of running processes.

Python3




# Import module
import wmi
  
# Initializing the wmi constructor
f = wmi.WMI()
  
flag = 0
  
# Iterating through all the running processes
for process in f.Win32_Process():
    if "chrome.exe" == process.Name:
        print("Application is Running")
        flag = 1
        break
  
if flag == 0:
    print("Application is not Running")


Output:

Application is Running

We import the wmi module. Then we search for chrome.exe in all running processes on the local machine by iterating through the process names. If it matches with the process. Name, it will print Application is Running, else Application is not Running.



Last Updated : 24 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads