python psutil库调用
时间: 2023-12-15 17:03:56 浏览: 186
psutil是一个Python第三方库,可以用来获取操作系统的信息,如CPU、内存、磁盘、网络等信息,还可以用来进行进程管理。下面是一个简单的psutil库调用的例子:
```python
import psutil
# 获取CPU信息
cpu_percent = psutil.cpu_percent(interval=1)
print("CPU使用率:{}%".format(cpu_percent))
# 获取内存信息
mem = psutil.virtual_memory()
print("总内存:{}MB".format(mem.total/1024/1024))
print("已用内存:{}MB".format(mem.used/1024/1024))
print("空闲内存:{}MB".format(mem.free/1024/1024))
# 获取磁盘信息
disk = psutil.disk_usage('/')
print("总磁盘空间:{}GB".format(disk.total/1024/1024/1024))
print("已用磁盘空间:{}GB".format(disk.used/1024/1024/1024))
print("剩余磁盘空间:{}GB".format(disk.free/1024/1024/1024))
# 获取网络信息
net_io_counters = psutil.net_io_counters()
print("发送字节数:{}".format(net_io_counters.bytes_sent))
print("接收字节数:{}".format(net_io_counters.bytes_recv))
```
上述代码演示了如何使用psutil库获取CPU、内存、磁盘、网络等信息。需要注意的是,使用psutil库需要先安装,可以使用pip命令进行安装:
```
pip install psutil
```
阅读全文
相关推荐

















