List device serial numbers in Python

nRF Util

tags
nRF Util

The following Python code snippet uses the JSON output to list the serial numbers of all connected devices:

import json
import subprocess

# List all devices
output = subprocess.run(["nrfutil", "device", "list", "--json", "--skip-overhead"],
                        capture_output=True)
stdout = output.stdout.decode("utf-8")
devices = json.loads(stdout)["devices"]

# Print the serial numbers of the devices
print([device["serialNumber"] for device in devices])