user8511634
user8511634

Reputation:

Python+CMD+tasklist (windows)

import os
os.system('tasklist')

How do I extract anything from this command? It shows all the tasks, but when I do print(os.system('tasklist')) it prints out 0. Is it possible to make this code useful?

Upvotes: 1

Views: 1142

Answers (1)

jpp
jpp

Reputation: 164683

This is an alternative way to access the data I think you are looking for:

import subprocess

subprocess.getoutput('tasklist').split('\n')

# ['',
#  'Image Name                     PID Session Name        Session#    Mem Usage',
#  '========================= ======== ================ =========== ============',
#  'System Idle Process              0 Services                   0         24 K',
#  'System                           4 Services                   0         44 K',
#  'smss.exe                       284 Services                   0         80 K',
#  'csrss.exe                      384 Services                   0      1,660 K',
#  'wininit.exe                    448 Services                   0        156 K',
#  'csrss.exe                      480 Console                    1     10,996 K',
#  'services.exe                   516 Services                   0      5,440 K',
#  'winlogon.exe                   540 Console                    1      2,196 K',
#  ...
# ]

Upvotes: 1

Related Questions