def ask_for_port():
 sys.stderr.write('\n--- Available ports:\n')
 ports = []
 for (n, (port, desc, hwid)) in enumerate(sorted(comports()), 1):
  sys.stderr.write('--- {:2}: {:20} {}\n'.format(n, port, desc))
  ports.append(port)
 while True:
  port = raw_input('--- Enter port index or full name: ')
  try:
   index = (int(port) - 1)
   if (not (0 <= index < len(ports))):
    sys.stderr.write('--- Invalid index!\n')
    continue
  except ValueError:
   pass
  else:
   port = ports[index]
  return port

def ask_for_port ( ) : sys . stderr . write ( '\n--- Available ports:\n' ) ports = [ ] for ( n , ( port , desc , hwid ) ) in enumerate ( sorted ( comports ( ) ) , 1 ) : sys . stderr . write ( '--- {:2}: {:20} {}\n' . format ( n , port , desc ) ) ports . append ( port ) while True : port = raw_input ( '--- Enter port index or full name: ' ) try : index = ( int ( port ) - 1 ) if ( not ( 0 <= index < len ( ports ) ) ) : sys . stderr . write ( '--- Invalid index!\n' ) continue except ValueError : pass else : port = ports [ index ] return port
show a list of ports and ask the user for a choice .