user12951028
user12951028

Reputation:

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, ...

Upvotes: 1

Views: 444

Answers (2)

T139
T139

Reputation: 140

import psutil
procs = [proc.name().replace('.exe', '') for proc in psutil.process_iter()]
print(', '.join(procs))

As mentioned by @Vicrobot, the print line could as well be replaced by

print(*procs, sep = ', ')

while keeping in mind that the default seperator of print is already ' '.

.

Upvotes: 1

Vicrobot
Vicrobot

Reputation: 3988

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.

Upvotes: 1

Related Questions