How can I return system information in Python?

后端 未结 7 1065
北荒
北荒 2020-12-01 02:44

Using Python, how can information such as CPU usage, memory usage (free, used, etc), process count, etc be returned in a generic manner so that the same code can be run on L

7条回答
  •  盖世英雄少女心
    2020-12-01 03:48

    Regarding cross-platform: your best bet is probably to write platform-specific code, and then import it conditionally. e.g.

    import sys
    if sys.platform == 'win32':
      import win32_sysinfo as sysinfo
    elif sys.platform == 'darwin':
      import mac_sysinfo as sysinfo
    elif 'linux' in sys.platform:
      import linux_sysinfo as sysinfo
    #etc
    
    print 'Memory available:', sysinfo.memory_available()
    

    For specific resources, as Anthony points out you can access /proc under linux. For Windows, you could have a poke around at the Microsoft Script Repository. I'm not sure where to get that kind of information on Macs, but I can think of a great website where you could ask :-)

提交回复
热议问题