How can I return system information in Python?(如何在 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 Script存储库.我不知道在哪里可以获得有关 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 中返回系统信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 中返回系统信息?


基础教程推荐
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01