Python中获取计算机的信息,包括操作系统和硬件信息
Python中获取计算机的信息,包括操作系统和硬件信息
需要用到的库:platform 和 psutil
1. 获取操作系统信息
import platform
def get_os_info():
return platform.system(), platform.release()
os_name, os_release = get_os_info()
print(f"操作系统: {os_name} {os_release}")
2. 获取详细的平台信息
def get_platform_info():
return platform.platform(aliased=True, terse=False)
print(get_platform_info())
3. 获取CPU信息
import platform
def get_cpu_info():
return platform.processor()
cpu = get_cpu_info()
print(f"处理器: {cpu}")
4. 获取CPU占用率
import psutil
def get_cpu_info_psutil():
cpu_percent = psutil.cpu_percent(interval=1)
return cpu_percent
print(f"CPU使用率: {get_cpu_info_psutil()}%")
5. 获取内存信息
import psutil
def get_memory_info():
mem = psutil.virtual_memory()
return mem.total, mem.used, mem.percent
total, used, percent = get_memory_info()
print(f"总内存: {total / (1024.0 ** 3):.2f} GB")
print(f"已用内存: {used / (1024.0 ** 3):.2f} GB")
print(f"内存使用率: {percent}%")
6. 获取当前使用的磁盘信息
import psutil
def get_disk_info():
disk_usage = psutil.disk_usage("/")
return disk_usage.total, disk_usage.used, disk_usage.free
total, used, free = get_disk_info()
print(f"总磁盘空间: {total / (1024.0 ** 3):.2f} GB")
print(f"已用磁盘空间: {used / (1024.0 ** 3):.2f} GB")
print(f"剩余磁盘空间: {free / (1024.0 ** 3):.2f} GB")
7. 获取所有磁盘驱动的的信息
import psutil
def get_all_disk_info():
disks = []
# 获取所有磁盘分区
for part in psutil.disk_partitions():
# 使用disk_usage获取每个分区的磁盘使用情况
usage = psutil.disk_usage(part.mountpoint)
disks.append({
'device': part.device,
'mountpoint': part.mountpoint,
'fstype': part.fstype,
'opts': part.opts,
'total': usage.total,
'used': usage.used,
'free': usage.free,
'percent': usage.percent
})
return disks
# 打印所有磁盘信息
for disk_info in get_all_disk_info():
print(f"设备: {disk_info['device']}")
print(f"挂载点: {disk_info['mountpoint']}")
print(f"文件系统类型: {disk_info['fstype']}")
print(f"挂载选项: {disk_info['opts']}")
print(f"总大小: {disk_info['total'] / (1024.0 ** 3):.2f} GB")
print(f"已用空间: {disk_info['used'] / (1024.0 ** 3):.2f} GB")
print(f"可用空间: {disk_info['free'] / (1024.0 ** 3):.2f} GB")
print(f"使用率: {disk_info['percent']}%")
print('---')
完整代码
# 获取硬件信息
# 编写:糖茶砌站(www.sg7z.com)
import psutil
import platform
# 获取操作系统信息
# 实例:操作系统: Windows 11
def get_os_info():
return platform.system(), platform.release()
os_name, os_release = get_os_info()
print(f"操作系统: {os_name} {os_release}")
# 获取详细的系统信息
# 实列:Windows-11-10.0.22631-SP0
def get_platform_info():
return platform.platform(aliased=True, terse=False)
print(get_platform_info())
# 获取CPU信息
# 实例:AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD
def get_cpu_info():
return platform.processor()
cpu = get_cpu_info()
print(f"处理器: {cpu}")
# 获取CPU使用率
# 实例:CPU使用率: 1.4%
def get_cpu_info_psutil():
cpu_percent = psutil.cpu_percent(interval=1)
return cpu_percent
print(f"CPU使用率: {get_cpu_info_psutil()}%")
# 获取内存信息
# 实例:总内存: 39.86 GB / 已用内存: 14.30 GB / 内存使用率: 35.9%
def get_memory_info():
mem = psutil.virtual_memory()
return mem.total, mem.used, mem.percent
total, used, percent = get_memory_info()
print(f"总内存: {total / (1024.0 ** 3):.2f} GB")
print(f"已用内存: {used / (1024.0 ** 3):.2f} GB")
print(f"内存使用率: {percent}%")
# 获取磁盘信息
# 描述:获取当前硬盘
# 实例:总磁盘空间: 931.51 GB / 已用磁盘空间: 423.65 GB / 剩余磁盘空间: 507.86 GB
def get_disk_info():
disk_usage = psutil.disk_usage("/")
return disk_usage.total, disk_usage.used, disk_usage.free
total, used, free = get_disk_info()
print(f"总磁盘空间: {total / (1024.0 ** 3):.2f} GB")
print(f"已用磁盘空间: {used / (1024.0 ** 3):.2f} GB")
print(f"剩余磁盘空间: {free / (1024.0 ** 3):.2f} GB")
# 获取硬盘分区信息
# 实例:
# ---
# 设备: E:\
# 挂载点: E:\
# 文件系统类型: NTFS
# 挂载选项: rw,fixed
# 总大小: 931.51 GB
# 已用空间: 423.65 GB
# 可用空间: 507.86 GB
# 使用率: 45.5%
# ---
def get_all_disk_info():
disks = []
# 获取所有磁盘分区
for part in psutil.disk_partitions():
# 使用disk_usage获取每个分区的磁盘使用情况
usage = psutil.disk_usage(part.mountpoint)
disks.append({
'device': part.device,
'mountpoint': part.mountpoint,
'fstype': part.fstype,
'opts': part.opts,
'total': usage.total,
'used': usage.used,
'free': usage.free,
'percent': usage.percent
})
return disks
# 打印所有磁盘信息
for disk_info in get_all_disk_info():
print(f"设备: {disk_info['device']}")
print(f"挂载点: {disk_info['mountpoint']}")
print(f"文件系统类型: {disk_info['fstype']}")
print(f"挂载选项: {disk_info['opts']}")
print(f"总大小: {disk_info['total'] / (1024.0 ** 3):.2f} GB")
print(f"已用空间: {disk_info['used'] / (1024.0 ** 3):.2f} GB")
print(f"可用空间: {disk_info['free'] / (1024.0 ** 3):.2f} GB")
print(f"使用率: {disk_info['percent']}%")
print('---')