Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Process is not found if opened until a lot of searches

import psutil

name = "notepad.exe"

for proc in psutil.process_iter():
    if proc.name().lower() == name.lower():
        print("found")
    elif proc.name().lower() != name.lower():
        print("not found")

output:

not found
not found
not found
found
not found
not found

"notepad.exe" was open from the start and until the end of the script.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It seems to me you could use any() rather than using a for loop:

if any(proc.name().lower() == name.lower() for proc in psutil.process_iter()):
    print("found")
else
    print("not found")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading