- 1 year ago
How to get CPU and RAM usage from windows machine using python?
You can use the psutil
library in Python to retrieve CPU and RAM usage information from a Windows machine. Here's how you can do it:
Installing the psutil
Library:
First, make sure you have the psutil
library installed. You can install it using the following command:
bashpip install psutil
Retrieving CPU and RAM Usage:
Here's an example script that demonstrates how to retrieve CPU and RAM usage information using the psutil
library:
pythonimport psutil
# Get CPU usage percentage
cpu_usage = psutil.cpu_percent(interval=1)
# Get RAM usage information
ram = psutil.virtual_memory()
ram_total = ram.total
ram_used = ram.used
ram_percent = ram.percent
print(f"CPU Usage: {cpu_usage}%")
print(f"RAM Total: {ram_total} bytes")
print(f"RAM Used: {ram_used} bytes")
print(f"RAM Usage: {ram_percent}%")
This script imports the psutil
library and then uses its functions to retrieve CPU and RAM usage information. The cpu_percent()
function returns the CPU usage percentage over a specified interval, and the virtual_memory()
function returns RAM usage information.
Remember to adjust the code according to your needs and include proper error handling in a real-world application. Also, note that CPU and RAM usage can vary based on system load, so the values you see may change over time.