如何使用Python返回系统信息? [英] How can I return system information in Python?

查看:114
本文介绍了如何使用Python返回系统信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python,如何以通用方式返回诸如CPU使用率,内存使用率(空闲,已使用等),进程计数等信息,以便可以在Linux,Windows,BSD等上运行相同的代码?

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 Linux, Windows, BSD, etc?

或者,只有在该操作系统确实是运行环境的情况下,如何在运行上述操作系统的代码的情况下,才能在上述所有系统上返回该信息?

Alternatively, how could this information be returned on all the above systems with the code specific to that OS being run only if that OS is indeed the operating environment?

推荐答案

关于跨平台:您最好的选择可能是编写平台特定的代码,然后有条件地将其导入.例如

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()

对于特定资源,如Anthony指出的那样,您可以在Linux下访问/proc.对于Windows,您可以在 Microsoft脚本中四处浏览存储库.我不确定从哪里可以在Mac上获得此类信息,但是我可以想到一个不错的网站,您可以在这里问到:-)

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 :-)

这篇关于如何使用Python返回系统信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆