Interview Prep/Python for DevOps/OS, filesystem, subprocess & CLIs

How do you get host information (hostname, CPU, memory, disk) from Python?

IntermediateConceptOS, filesystem, subprocess & CLIs
Answer
import os, platform, shutil, socket
socket.gethostname(); platform.system(); platform.release()
os.cpu_count(); os.getloadavg()
shutil.disk_usage("/")          # total, used, free

For memory and per-process stats use psutil: psutil.virtual_memory().percent, psutil.process_iter(["pid", "name", "memory_info"]), psutil.net_connections(). In containers, remember os.cpu_count() reports host CPUs, not your cgroup quota; read /sys/fs/cgroup/cpu.max or use os.process_cpu_count() (3.13) for affinity-aware counts.

All Python for DevOps questions Roadmap: Python for automation

More on OS, filesystem, subprocess & CLIs