title: What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?
body: <p><strong>Note:</strong> I'm using Python 2.7, and pySerial for serial communications.</p>
  
  <p>I found this article which lists two ways: <a href="http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports" rel="nofollow noreferrer">http://www.zaber.com/wiki/Software/Python#Displaying_a_list_of_available_serial_ports</a></p>
  
  <p>This method works on Windows and Linux, but sometimes misses virtual ports on Linux:</p>
  
  <pre><code>import serial
  
  def scan():
     # scan for available ports. return a list of tuples (num, name)
     available = []
     for i in range(256):
         try:
             s = serial.Serial(i)
             available.append( (i, s.portstr))
             s.close()
         except serial.SerialException:
             pass
     return available
  
  print "Found ports:"
  for n,s in scan(): print "(%d) %s" % (n,s)
  </code></pre>
  
  <p>And this one that only works on Linux, but includes virtual ports:</p>
  
  <pre><code>import serial, glob
  
  def scan():
     # scan for available ports. return a list of device names.
     return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*')
  
  print "Found ports:"
  for name in scan(): print name
  </code></pre>
  
  <p>I suppose I could do platform detection to use the second method (the one that includes virtual ports) when running on Linux, and the first method when running Windows, but what about Mac?</p>
  
  <p>How should I enumerate serial ports (virtual too) regardless of platform?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I found a few pertinent questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1659283/macpython-programmatically-finding-all-serial-ports">MacPython: programmatically finding all serial ports</a></li>
  <li><a href="https://stackoverflow.com/questions/8632586/macos-whats-the-difference-between-dev-tty-and-dev-cu">MacOS: what&#39;s the difference between /dev/tty.* and /dev/cu.*?</a></li>
  <li><a href="https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them">How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?</a></li>
  </ul>