How to check if an application is open in Python?

Published on Aug. 22, 2023, 12:15 p.m.

To check if an application is open in Python, you can use the psutil library, which provides an easy way to gather information about running processes on your system. Here’s an example:

import psutil

# Specify the name of the application
app_name = "chrome.exe"

# Iterate over all running processes
for process in psutil.process_iter(['name']):
    if process.info['name'] == app_name:
        print(app_name, "is running")
        break
else:
    print(app_name, "is not running")

In this example, we specify the name of the application as a string variable called app_name. We then iterate over all running processes using the psutil.process_iter() method and check if any of them have the same name as the application we’re looking for. If we find a match, we print a message indicating that the application is running, and exit the loop using the break statement. If we reach the end of the loop without finding a match, we print a message indicating that the application is not running.

Note that the psutil library is not included in the standard Python distribution, so you may need to download and install it separately. You can do this using pip, the Python package manager, by running pip install psutil in a command prompt or terminal window.

Tags: