What is wrong with my Python code which detects whether an application (Chrome) is opened or not?

37 views Asked by At
import psutil as psutil

for proc in psutil.process_iter():
    proc_name = proc.name()
    if proc_name == 'chrome.exe':
        print('chrome is running now.')`

When I run the above Python code that checks whether an application like Chrome is opened or not on a MacBook M1 after opening Chrome, it does not print the correct result (i.e. Chrome is running now.)

1

There are 1 answers

0
SIGHUP On

For macOS (it will be different on Windows):

import psutil

for proc in psutil.process_iter():
    if proc.name() == "Google Chrome":
        print("Chrome is running")
        break
else:
    print("Chrome is not running")