Linux and Python: auto-detect Arduino serial port
arduino, macos, python, serial-port
Solution
First of all, if you're using a shell, you can use a glob (`*`), so your command would become `ls /dev/tty.usbmodem*`.
Next, you don't even have to call a shell command to use a glob in Python!
Consider the following code:
import glob
print(glob.glob("/dev/tty.usbmodem*"))
Problem
I have a problem automagically detecting my Arduino's serial port in Python, using Mac/Linux. I know a working shell command to find the port; because Arduino serial ports almost always begin with `tty.usbmodem`, you can find the serial port with `ls /dev | grep tty.usbmodem` which should return something like `tty.usbmodem262141`. However, I'm confused on how to call this shell command from my Python code. I've tried this: ``` p = "/dev/" + str(subprocess.Popen('ls /dev | grep tty.usbmodem', shell=True).stdout) ``` Which should make `p` become `/dev/tty.usbmodem262141`. However, at the moment I get `/dev/None`. How can I modify my shell script call to return the right string? I've tried to use several commands to call shell scripts, but none have worked.