Reputation: 3
Im trying to find percular process running on my system. For that pulling all the processes that are running on my system at the moment and storing it in a variable called 'name'. Now I want to parse through throught data stored in this 'name' variable and find a perticular process that I need.
import subprocess
output = subprocess.check_output(('TASKLIST', '/FO', 'CSV')).decode()
# get rid of extra " and split into lines
output = output.replace('"', '').split('\r\n')
keys = output[0].split(',')
proc_list = [i.split(',') for i in output[1:] if i]
# make dict with proc names as keys and dicts with the extra nfo as values
proc_dict = dict((i[0], dict(zip(keys[1:], i[1:]))) for i in proc_list)
for name, values in sorted(proc_dict.items(), key=lambda x: x[0].lower()):
print(name)
This is my code.
Upvotes: 0
Views: 25
Reputation: 31
I think I didn't get your question, yet. If you loop through all of your processes, you could check in every loop through the name of the current process with the process you are looking for. Maybe not only a simple comparison, but with the use of regex.
For example with process WhatsApp.exe:
import subprocess
import re
output = subprocess.check_output(('TASKLIST', '/FO', 'CSV')).decode()
# get rid of extra " and split into lines
output = output.replace('"', '').split('\r\n')
keys = output[0].split(',')
proc_list = [i.split(',') for i in output[1:] if i]
# make dict with proc names as keys and dicts with the extra nfo as values
proc_dict = dict((i[0], dict(zip(keys[1:], i[1:]))) for i in proc_list)
for name, values in sorted(proc_dict.items(), key=lambda x: x[0].lower()):
proc = re.search("[wW]hats[Aa]pp", name)
if proc is not None:
print(name)
Another way is an comparison without the ".exe" and the process in lower case. for name, values in sorted(proc_dict.items(), key=lambda x: x[0].lower()):
if name[:-4].lower() == "whatsapp":
print(name)
The second way could be dangerous, if you are looking for a process which is not a .exe (if there is something like that in windows)
Upvotes: 1