import os ports = [x for x in os.listdir("/dev") if x.startswith("ttyUSB") or x.startswith("ttyACM")] if ports and isinstance(ports[0], str): print(ports[0]) else: print("Error: Unable to find a ttyUSB/ttyACM serial port") raise SystemExit(1)