Automatically Generate Hardware Inventory Reports in Python
Generate a system hardware report including OS version, CPU cores, RAM, and disk usage using platform and psutil.
Requires third-party packages — install first
pip install psutil
Python code
18 linesimport platform
import psutil # requires: pip install psutil
from datetime import datetime
def generate_hardware_report():
report_lines = []
report_lines.append(f"Report Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
report_lines.append(f"System: {platform.system()} {platform.release()} ({platform.version()})")
report_lines.append(f"Processor: {platform.processor()}")
report_lines.append(f"CPU Cores: {psutil.cpu_count(logical=False)} physical, {psutil.cpu_count(logical=True)} logical")
report_lines.append(f"Total RAM: {psutil.virtual_memory().total / (1024**3):.2f} GB")
disk_info = psutil.disk_usage('/')
report_lines.append(f"Disk (/): {disk_info.total / (1024**3):.2f} GB total, {disk_info.free / (1024**3):.2f} GB free")
report_lines.append("-" * 40)
return "\n".join(report_lines)
if __name__ == "__main__":
print(generate_hardware_report())
Output
Report Generated: 2025-04-07 14:30:22
System: Windows 10 (10.0.19045)
Processor: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
CPU Cores: 4 physical, 8 logical
Total RAM: 15.86 GB
Disk (/): 237.43 GB total, 112.78 GB free
----------------------------------------
How it works
This script uses platform (stdlib) to capture OS and processor details and psutil (third-party) for hardware resource data. datetime stamps the report. The function assembles a formatted string with each hardware metric on its own line.
Common mistakes
- Forgetting to install psutil via pip before running the code.
- Assuming disk path '/' works on Windows — on that OS use the drive letter like 'C:' .
- Calling psutil virtual memory methods on unsupported platforms without a fallback.
Variations
- Export the report as a plain-text file instead of printing it.
- Add network interface info with `psutil.net_if_addrs()` for more complete inventory.
Real-world use cases
- Generating a daily inventory report for IT asset management across a fleet of servers.
- Checking hardware specs on a remote machine before deploying a resource-heavy application.
- Collecting system telemetry from developer workstations for capacity planning.
Sponsored
Sponsored
Reserved space — layout preview until AdSense is connected
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
- Benchmark File Read and Write Speed in Python medium
Keep learning
Related tutorials and quizzes for this topic.