Search code examples
pythonlistloopsprocesswmic

check if any item exist in the list (python)


im coding a tool to get the list of windows process ... and then search for any internet browser (process name) like chrome.exe , opera.exe .... etc if found one of them then do something ... if didn't just print nothing found my code :

import os 
import time

def qa():
    home = os.environ.get("HOMEDRIVE")
    lol =home +"/"
    mycurrent = os.getcwd()
    change = os.chdir(lol)
    mycurrent = os.getcwd()



    d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower()

    lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
    for q in lists:

        if any(item in q for item in d) :


            qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
            print qassam
            dir1 = os.path.dirname(qassam)
            print dir1


            #CreateShortCut_to_Desktop(qassam , dir1)
            print time.time()




        else:
            print "nothing found"
            break
qa()

i killed all the browser process and i run the code it should gives me nothing found but ... he keep loop and run this block of code :

 qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
            print qassam
            dir1 = os.path.dirname(qassam)
            print dir1


            #CreateShortCut_to_Desktop(qassam , dir1)
            print time.time()

and this is my output :

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe C:\Program Files (x86)\Google\Chrome\Application 1538829033.16 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe C:\Program Files (x86)\Google\Chrome\Application 1538829041.34 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe C:\Program Files (x86)\Google\Chrome\Application 1538829048.94 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe C:\Program Files (x86)\Google\Chrome\Application


Solution

  • if any(item in q for item in d) :is not doing what you think it does.

    The issue with your if command is that you have a for loop first. The q are the individual strings that you want to check against, so item in q is actually checking that the string (as the object string) is inside one of the element of the q string (so checking one element after the other), which cannot work. I would suggest to split each tested string to keep the filename.

    Then for item in d is not doing what you think either. It loops one character after the other int he string as well. You may want to split the string first, so the end result could be something like:

    d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower().split()
    
    lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
    if any(item.split(os.sep)[-1] in lists for item in d) :
    

    May not be perfect, but it's a start.