🛠 Premier script Python : inventaire du système
Objectives
Write a first Python script that displays the inventory of the Raspberry Pi 3 A+: BCM2837B0 SoC (4 Cortex-A53 cores @ 1.4 GHz), 512 MB of RAM shared between CPU/GPU, WiFi 2.4/5 GHz, and the time — the board has no real-time clock, so the time comes from NTP via WiFi at each boot.
Hardware
- Raspberry Pi 3 A+ with Raspberry Pi OS Lite and an established WiFi connection (this model has no Ethernet port)
- An editor (
nano) over SSH; Python 3 is preinstalled
Complete code
#!/usr/bin/env python3
# inventaire.py — inventaire matériel du 3 Model A+
import platform, re, shutil, subprocess, datetime
def cpu:
txt = open("/proc/cpuinfo").read
model = re.search(r"Model\s*:\s*(.+)", txt)
ncpu = len(re.findall(r"processor", txt))
return (model.group(1).strip if model else "?"), ncpu
def temp:
out = subprocess.run(["vcgencmd", "measure_temp"], capture_output=True, text=True)
return out.stdout.strip.removeprefix("temp=")
mod, n = cpu
mem = shutil.disk_usage("/")
print("Modele :", mod) # attendu : Raspberry Pi Model B+
print("Coeurs CPU :", n) # attendu : 4 (Cortex-A53 @ 1,4 GHz)
print("RAM : 512 Mo LPDDR2 (partagee CPU/GPU)")
print("Temp SoC :", temp)
print("Heure :", datetime.datetime.now, "(NTP, pas de RTC embarque)")
print(f"SD racine : {mem.used//2**30} Go / {mem.total//2**30} Go")
chmod +x inventaire.py && ./inventaire.py
Expected output
The script displays a Raspberry Pi model, 4 processors, a typical temperature of 35–50 °C, and an up-to-date date only if WiFi/NTP has synchronized the clock (otherwise a date close to Jan 1, 1970).
Common errors
- Incorrect time after an offline reboot: no RTC or battery, so NTP is required or you must wait for the WiFi connection.
vcgencmdnot found: this command is specific to Raspberry Pi OS and is absent from third-party distros (Ubuntu, Alpine).- Forgetting the
#!/usr/bin/env python3line and thechmod +xbefore running./inventaire.py. - Confusing RAM (512 MB) and SD card space: these are two very different figures.
Other tutorials for this board
- Allumer une LED en C avec libgpiod sur le GPIO 40 broches
- Bouton poussoir et retour console avec gpiozero
- Variation d'intensité d'une LED avec le PWM matériel
- Capteur I2C : température et pression avec BME280
- Connexion WiFi station et administration en SSH
- Démarrage SD/USB et récupération d'une carte qui ne boot plus
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython