The psutil
library is a Python module that provides an interface for retrieving system information and managing processes on various operating systems. It’s a powerful tool for system monitoring, administration, and automation. psutil
stands for “process and system utilities.”
Some of the core functionalities of the psutil
library include:
- Process Management: You can use
psutil
to interact with and manipulate running processes on your system. This includes functionalities like listing processes, getting information about a specific process, terminating processes, and more. - System Information: You can gather a wide range of system information, such as CPU usage, memory usage, disk usage, network statistics, and more.
- Sensor Information:
psutil
can access hardware sensors (like temperature and fan speed) information when available, although this feature might be limited to certain platforms and hardware configurations.
Here’s a simple example of how to use the psutil
library to retrieve some system information:
import psutil
# Get CPU information
cpu_info = psutil.cpu_percent(interval=1, percpu=True)
print("CPU Usage:", cpu_info)
# Get memory information
mem_info = psutil.virtual_memory()
print("Memory Usage:", mem_info)
# List running processes
process_list = psutil.process_iter(attrs=['pid', 'name'])
for process in process_list:
print(process.info)
# Get disk information
disk_info = psutil.disk_usage('/')
print("Disk Usage:", disk_info)
In this example, we import psutil
and then use it to obtain CPU usage, memory usage, list running processes, and get disk usage statistics. You can also perform more advanced tasks like monitoring specific processes, querying network information, and much more.
Keep in mind that psutil
is cross-platform, meaning it works on various operating systems such as Windows, Linux, macOS, and more. It’s a valuable library for system administrators, developers, and anyone who needs to interact with system resources and processes from within Python.