import psutil
import time
def readProcessList(processList):
for process in processList:
checkIfProcessRunning(process)
def showProcesses(procesList):
for process in procesList:
findProcessIdByName(process)
def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
# Iterate over the all the running process
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
print('Yes a '+ processName +' process was running')
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
print('No a' + processName + ' was not running')
pass
return False;
def findProcessIdByName(processName):
'''
Get a list of all the PIDs of a all the running process whose name contains
the given string processName
'''
listOfProcessObjects = []
# Iterate over the all the running process
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# Check if process name contains the given name string.
if processName.lower() in pinfo['name'].lower():
listOfProcessObjects.append(pinfo)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return listOfProcessObjects;
def main():
print("*** Check if a process is running or not ***")
Я хочу использовать этот список для сортировки моих результатов
processList = ["skype","chrome","windows"]
readProcessList(processList)
print("*** Find PIDs of a running process by Name ***")
# Find PIDs od all the running instances of process that contains 'chrome' in it's name
listOfProcessIds = findProcessIdByName('chrome')
if len(listOfProcessIds) > 0:
print('Process Exists | PID and other details are')
for elem in listOfProcessIds:
processID = elem['pid']
processName = elem['name']
processCreationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(elem['create_time']))
print((processID ,processName,processCreationTime ))
else :
print('No Running Process found with given text')
if __name__ == '__main__':
main()
Это мой код, я пытаюсь заставить DEF findProcessIDByname (имя процесса) использовать мой список процессов Processlist = chrome, Skype, windows. он должен использовать этот список, чтобы показать только те текущие процессы. эта программа показывает запущенные процессы на вашем компьютере. Я хочу, чтобы это отфильтровало результат с моим списком