8

When I enter Task Manager I can see two sections. Apps(3) and Background processes(94). What I'm trying to do is get only those 3 that are not in the background.

My code:

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

But this gives me the names of all of those 90+ apps running in both foreground and background. How do I separate one from the other?

Harplife
  • 37
  • 7
pantank14
  • 175
  • 1
  • 10
  • *"separate one from the other?"*: There is no way to do this beforehand. Use `for p in psutil.process_iter(attrs=['name', 'username'])` and filter by `username`. – stovfl Mar 22 '19 at 10:02
  • 1
    @stovfl But I would personally need to know the name of the foreground apps. What I want is for my program to recognize them. My ultimate goal is to make a function that closes all foreground running apps. There is no way to do this? – pantank14 Mar 22 '19 at 14:58
  • *"need to know the name"*: As you see in my comment example, there is a `name` field too. Read [filtering-and-sorting-processes](https://psutil.readthedocs.io/en/latest/#filtering-and-sorting-processes) – stovfl Mar 22 '19 at 16:31
  • 1
    @stovfl Please re-read my question. I know how to get the name of the process. What I don't know is how to differentiate one process from another based on whether they're running the background or foreground. What I mean by foreground is the ones you see opened in the bottom of taskbar. When I filter based on username, I get processes which are not on my taskbar too. – pantank14 Mar 22 '19 at 16:51

0 Answers0