I have a simple piece of code taken mostly from this answer, with some adjustments:
import psutil
try:
    if "firefox.exe" in (p.name() for p in psutil.process_iter()):
        print('Firefox is running')
    else:
        print('Firefox is not running')
except Exception as e:
    print(e)
If Firefox is running when I run this code, it prints the expected Firefox is running. But if Firefox isn't running, I get psutil.AccessDenied (pid=9092) instead of the expected Firefox is not running.
I also noticed that if firefox.exe is mis-spelled, I get the AccessDenied error again. I could just print('Firefox is not running') inside the except block, but that doesn't seem very smart.
Does anyone know why this is happening?
 
     
     
    