2

In C#, I have:

Process.GetProcessesByName

I'm looking for something like that in Python?

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • If you're in Unix, an easy way is using `subprocess` to spawn `ps`. Also navigating `/proc` – salezica Mar 02 '14 at 19:18
  • possible duplicate of [Process list on Linux via Python](http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python) – Valentin Lorentz Mar 02 '14 at 19:20
  • Could depend heavily on your OS and or Python version. Please post your specs. – anon582847382 Mar 02 '14 at 19:21
  • i in windows and i looking for an easy way – Yonatan Shorani Mar 02 '14 at 19:43
  • try : To get the processes in win platform : http://stackoverflow.com/questions/1632234/python-list-running-processes-64bit-windows to get the processes in linux : http://stackoverflow.com/questions/160245/which-is-the-best-way-to-get-a-list-of-running-processes-in-unix-with-python to change the pid to name : http://stackoverflow.com/questions/4189717/get-process-name-by-pid – esnadr Mar 02 '14 at 19:50

1 Answers1

1

You can use psutil module:

It currently supports Linux, Windows, OSX, FreeBSD, Sun Solaris both 32-bit and 64-bit with Python versions from 2.4 to 3.3 by using a single code base.

First install it:

pip install psutil

Then do similar what Process.GetProcessesByName method do:

Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name.

Code:

import psutil

def get_processes_by_name(name):
    return [process for process in psutil.process_iter() if process.name == name]


print(get_processes_by_name('python'))

Output:

[<psutil.Process(pid=10217, name='python') at 44007184>, 
 <psutil.Process(pid=10223, name='python') at 44007312>
]
Omid Raha
  • 9,862
  • 1
  • 60
  • 64