Ejercicio
Recuperar Información Del Sistema
Objectivo
Desarrollar un programa Python que recupere y muestre información del sistema, como el sistema operativo, detalles de la CPU, uso de la memoria y espacio en disco. El programa debe utilizar bibliotecas como platform, psutil u os para recopilar esta información y presentarla en un formato fácil de usar. Incluir manejo de errores para sistemas no compatibles o bibliotecas faltantes.
Ejemplo de ejercicio de Python
Mostrar código Python
import platform
import psutil
import os
def get_system_info():
"""Retrieve and display system information."""
# Get operating system information
try:
os_info = platform.system()
os_version = platform.version()
os_release = platform.release()
except Exception as e:
print(f"Error retrieving OS information: {e}")
os_info = os_version = os_release = "N/A"
# Get CPU information
try:
cpu_count = psutil.cpu_count(logical=True)
cpu_percent = psutil.cpu_percent(interval=1) # Get CPU usage percentage
except Exception as e:
print(f"Error retrieving CPU information: {e}")
cpu_count = cpu_percent = "N/A"
# Get memory usage information
try:
memory = psutil.virtual_memory()
memory_total = memory.total / (1024 ** 3) # Convert to GB
memory_used = memory.used / (1024 ** 3) # Convert to GB
memory_percent = memory.percent
except Exception as e:
print(f"Error retrieving memory information: {e}")
memory_total = memory_used = memory_percent = "N/A"
# Get disk space information
try:
disk = psutil.disk_usage('/')
disk_total = disk.total / (1024 ** 3) # Convert to GB
disk_used = disk.used / (1024 ** 3) # Convert to GB
disk_free = disk.free / (1024 ** 3) # Convert to GB
except Exception as e:
print(f"Error retrieving disk information: {e}")
disk_total = disk_used = disk_free = "N/A"
# Display the system information
print("\nSystem Information:")
print(f"Operating System: {os_info} {os_release} ({os_version})")
print(f"CPU: {cpu_count} cores, {cpu_percent}% usage")
print(f"Memory: {memory_total:.2f} GB Total, {memory_used:.2f} GB Used, {memory_percent}% Used")
print(f"Disk: {disk_total:.2f} GB Total, {disk_used:.2f} GB Used, {disk_free:.2f} GB Free")
def main():
"""Main function to display system information."""
try:
get_system_info()
except Exception as e:
print(f"Error in retrieving system information: {e}")
if __name__ == "__main__":
main()
Output
System Information:
Operating System: Windows 10 Pro (10.0.19042)
CPU: 8 cores, 23.4% usage
Memory: 16.00 GB Total, 8.56 GB Used, 53% Used
Disk: 500.00 GB Total, 150.00 GB Used, 350.00 GB Free
Código de ejemplo copiado
Comparte este ejercicio de Python