Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find any item in the list
#1
hello all ...

im coding a program to get the internet browsers names and dir's from the windows if he found the process ...
this is my code :

import os 
import time

def q():
    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
q()
the d variable run this command (wmic process get name | findstr /v "Name 'System Idle Process' System) on the system to get all the process ... i need to modify the code search in this process if he found any item from list name lists[] then run this lines :
qassam = os.popen("dir /s /b {}".format(lists[1])).read().strip()
print qassam
dir1 = os.path.dirname(qassam)
print dir1
if he did not found any browser just print found nothing
Reply
#2
You have to clean up what you get from d = os.popen.
d will be a big sting with mixed in \n \r and empty stings.

You should use subprocess,and not os.popen() which is deprecated.
Quote:Deprecated since version 2.6: All of the os.popen*() functions are obsolete. Use the subprocess module.

If doing it subprocess and can use check_output to get output.
Ideally would like to remove shell=True,but command did like to be spilt out to a list.
import subprocess

pro = subprocess.check_output("""wmic process get name | findstr /v "Name 'System Idle Process' System""", shell=True)
pro_string = pro.decode('utf-8')

lst = []
for item in pro_string.split('\r'):
    lst.append(item.strip())
process_list = filter(None, lst)
process_list = list(process_list)
#print(process_list)
Test output,see now that have clean up list with processes.
>>> process_list[:6]
['Registry',
 'smss.exe',
 'csrss.exe',
 'wininit.exe',
 'csrss.exe',
 'winlogon.exe']

>>> pro_find = ['chrome.exe', 'smss.exe']
>>> any(p in process_list for p in pro_find)
True 
Alternative use psutil is good for this kind of stuff.
Reply
#3
(Oct-07-2018, 09:45 AM)snippsat Wrote: You have to clean up what you get from d = os.popen.
d will be a big sting with mixed in \n \r and empty stings.

You should use subprocess,and not os.popen() which is deprecated.
Quote:Deprecated since version 2.6: All of the os.popen*() functions are obsolete. Use the subprocess module.

If doing it subprocess and can use check_output to get output.
Ideally would like to remove shell=True,but command did like to be spilt out to a list.
import subprocess

pro = subprocess.check_output("""wmic process get name | findstr /v "Name 'System Idle Process' System""", shell=True)
pro_string = pro.decode('utf-8')

lst = []
for item in pro_string.split('\r'):
    lst.append(item.strip())
process_list = filter(None, lst)
process_list = list(process_list)
#print(process_list)
Test output,see now that have clean up list with processes.
>>> process_list[:6]
['Registry',
 'smss.exe',
 'csrss.exe',
 'wininit.exe',
 'csrss.exe',
 'winlogon.exe']

>>> pro_find = ['chrome.exe', 'smss.exe']
>>> any(p in process_list for p in pro_find)
True 
Alternative use psutil is good for this kind of stuff.
great its working but now how i cant now which of pro_find = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
exist and pass it name to
qassam = os.popen("dir /s /b {}".format("pass the process name founded from the list ")).read().strip()
Reply
#4
(Oct-07-2018, 09:58 AM)evilcode1 Wrote: great its working but now how i cant now which of pro_find = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]
exist
Can compare lists,set() is best as there can be several process with same name.
>>> process_list[:6]
['Registry',
 'smss.exe',
 'csrss.exe',
 'wininit.exe',
 'csrss.exe',
 'winlogon.exe']
>>> pro_find =  ['csrss.exe', 'smss.exe', "firefox.exe"]
>>> p = set(pro_find) & set(process_list)
>>> p
{'csrss.exe', 'smss.exe'}
So firefox.exe is not running.
Quote:exist and pass it name to
I don't understand what you try to do here,it just process names a dir will not work without a absolute path.
Should you be using os.popen() Snooty
Reply
#5
i will explain in details ....
lets say i run chrome.exe and the code found that im running chrome .. how i can print which of ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"] found !! i mean i need to print the name of process from the list that the code found it running not a position


i hope u understand me
Reply
#6
Doesn't may example not do what you explain?
Let take you list and run on my system now.
>>> pro_find = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"] 
>>> p = set(pro_find) & set(process_list)
>>> p
{'chrome.exe'} 
So it find that i have Chrome running,and none of the other is running.
If i start FireFox.
>>> p = set(pro_find) & set(process_list)
>>> p
{'firefox.exe', 'chrome.exe'}
What's not running.
>>> p = set(pro_find) - set(process_list)
>>> p
{'opera.exe', 'microsoftedgecp.exe', 'iexplore.exe'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program to find Mode of a list PythonBoy 6 1,000 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,049 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,157 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,372 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,907 May-31-2022, 01:58 PM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,819 Jan-23-2022, 07:20 PM
Last Post: menator01
  how to easily create a list of already existing item CompleteNewb 15 3,387 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,556 Nov-11-2021, 06:43 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020