0

Trying to display psutil values thus:-

  for VM psutil.virtual_memory():
      print (VM)

This prints the values but not the names. Any suggestions?

kdth
  • 3
  • 2
  • Thanks DS & GR: I managed to list item by item using ._fields (why is it naughty?); but will go with ._asdict() method as more appealing. KDT – kdth Nov 06 '20 at 10:11

2 Answers2

1

Just print it without iterating over it, for debugging and development purposes. This looks like:

svmem(total=16521965568, available=12267257856, percent=25.8, used=3729342464, free=3388936192, active=5169664000, inactive=3971186688, buffers=954048512, cached=8449638400, shared=181248000, slab=3778879488)

It's a named tuple, not a dict, so you're not really supposed to iterate over the "keys". However, there's a slightly naughty ._fields you could use if you really have your heart set on getting a tuple of attribute names.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
1
import psutil
for k, v in psutil.virtual_memory()._asdict().items():
    print(k, v)

...prints:

total 16706494464
available 7661907968
percent 54.1
used 7520182272
free 2695786496
active 9574060032
inactive 3523575808
buffers 896020480
cached 5594505216
shared 1172226048
slab 464887808
Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60