获取电脑 操作系统、CPU、内存、磁盘等信息。
pip install platform
pip install psutil
通过使用platform
库获取基本的操作系统和处理器信息,并使用psutil
库检索更多详细的系统资源状态,如CPU频率、内存状态和磁盘使用情况。
调用get_computer_config()
自定义函数将返回一个包含全部相关配置信息的字典,该字典可以在其它程序中供进一步处理或显示。
import platform
import psutil
def get_computer_config():
# 获取操作系统信息
os_info = platform.uname()
system = os_info.system
node_name = os_info.node
release = os_info.release
version = os_info.version
machine = os_info.machine
processor = os_info.processor
# 获取CPU信息
cpu_count = psutil.cpu_count(logical=True)
cpu_freq = psutil.cpu_freq()
# 获取内存信息
virtual_memory = psutil.virtual_memory()
total_memory = virtual_memory.total
available_memory = virtual_memory.available
# 获取磁盘信息
disk_info = psutil.disk_partitions()
disk_usage = psutil.disk_usage('/')
# 整理配置信息
config = {
'system': system,
'node_name': node_name,
'release': release,
'version': version,
'machine': machine,
'processor': processor,
'cpu_count': cpu_count,
'cpu_freq': cpu_freq,
'total_memory': total_memory,
'available_memory': available_memory,
'disk_partitions': disk_info,
'disk_usage': disk_usage,
}
return config
# 示例使用
if __name__ == "__main__":
config = get_computer_config()
print("Computer Configuration:")
for key, value in config.items():
print(f"{key}: {value}")
标签:info,psutil,python,电脑,获取,memory,disk,config,cpu
From: https://www.cnblogs.com/lanjianhua/p/18552842