Issue
I would like to retrieve the stats (Ram, storage, ...) of my raspberry pi in a python code but I can't find how. An idea ?
Solution
Consider using the psutil library to assist you with this.
Have a look at the below code snippet which I copied verbatim from this page:
import psutil
# Get cpu statistics
cpu = str(psutil.cpu_percent()) + '%'
# Calculate memory information
memory = psutil.virtual_memory()
# Convert Bytes to MB (Bytes -> KB -> MB)
available = round(memory.available/1024.0/1024.0,1)
total = round(memory.total/1024.0/1024.0,1)
mem_info = str(available) + 'MB free / ' + str(total) + 'MB total ( ' + str(memory.percent) + '% )'
# Calculate disk information
disk = psutil.disk_usage('/')
# Convert Bytes to GB (Bytes -> KB -> MB -> GB)
free = round(disk.free/1024.0/1024.0/1024.0,1)
total = round(disk.total/1024.0/1024.0/1024.0,1)
disk_info = str(free) + 'GB free / ' + str(total) + 'GB total ( ' + str(disk.percent) + '% )'
print("CPU Info–> ", cpu)
print("Memory Info–>", mem_info)
print("Disk Info–>", disk_info)
Answered By - Reegz Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.