How to get a list of processes running windows in python?

3

I would like to know how to get the processes running from windows and store them in a list in python.

from os import system
system('tasklist')

I know that the code above shows the processes, but I would like to have them put in a list even if another library is used.

    
asked by anonymous 05.01.2017 / 17:38

2 answers

4

For a cross-platform solution, use the psutil package.

1 - Install it from github or (easier) with pip:

pip install psutil

2 - Run the following sample program:

import psutil as ps

print('Lista de processos em execução:')
for proc in ps.process_iter():
    info = proc.as_dict(attrs=['pid', 'name'])
    print('Processo: {} (PID: {})'.format(info['pid'], info['name']))

It lists the data (name and pid) of the processes one by one, obtained through a dictionary. But if you want to build a list with only the names, for example, just do:

processos = [proc.name() for proc in ps.process_iter()]

The list with the process information access methods you can use (in the proc variable, shown in the code above) is in the documentation of class Process .

    
05.01.2017 / 18:47
2

I found here for a possible solution:

import wmi
c = wmi.WMI ()

for process in c.Win32_Process():
    print(process.ProcessId, process.Name)

Reference

I did not test because I do not have windows, but if it does not work I take the answer back with no problem

    
05.01.2017 / 17:40