Search code examples
pythonpsutil

How to get all processes to a string?


There is the following code, how to display all processes in it with one line and remove from each .exe?

import psutil
for proc in psutil.process_iter():
    name = proc.name()
    print(name)

to get it

chrome, opera, svhost, ...

Solution

  • To get them in one line, use sep parameter in print function:

    import psutil
    enlisted = [proc.name() for proc in psutil.process_iter()]
    print(*enlisted, sep = ' ')
    

    Or there is end parameter too.