-2

I want a program in python that can get a list of all of the programs running, like all the .exe's, and I want to be able to get them into a variable or array so that I can maybe use a for loop to go through them and filter ones to be shut-down, this is what I've got below to try and get the tasklist using the tasklist command in python, any ideas on how to get all of the task names that the command prompt gives me into an array or variable

    import os 
    def shut():
        return(os.system('tasklist'))
    print(shut())

2 Answers2

1

Here is a simple example using Python 3 and tasklist:

import subprocess

proc = subprocess.Popen('tasklist', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, error) = proc.communicate()

if error is not None:
    print("error:", error.decode())

print("output:", output.decode())

The decodes are required because byte objects are returned.

cdarke
  • 42,728
  • 8
  • 80
  • 84
0

You can use the check_output function:

from subprocess import check_output
var = check_output('tasklist')