title: Using Python to find drive letter (Windows)
body: <p>I'm trying to write a python script (I'm a newbie) that will search the root directory of each connected drive on Windows for a key file and then return the drive letter it's on setting a variable as the drive letter.</p>
  
  <p>Currently I have:</p>
  
  <pre><code>import os
  if os.path.exists('A:\\File.ID'):
          USBPATH='A:\\'
          print('USB mounted to', USBPATH)
      if os.path.exists('B:\\File.ID'):
          USBPATH='B:\\'
          print('USB mounted to', USBPATH)
      if os.path.exists('C:\\File.ID'):
  </code></pre>
  
  <p>-- And then recurring for every drive letter A through Z. Naturally this will be a lot to type out and I'm just wondering if there's a workaround to keep my code tidy and as minimal as possible (or is this the only way?).</p>
  
  <p>Additionally, is there a way to have it print an error if the drive isn't found (IE say please plug in your USB) and then return to the start/loop? Something like</p>
  
  <pre><code>print('Please plug in our USB drive')
  return-to-start
  </code></pre>
  
  <p>Kind of like a GOTO command-prompt command?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>For people with similar future inquiries, here's the solution:</p>
  
  <pre><code>from string import ascii_uppercase
  import os
  
  
  def FETCH_USBPATH():
      for USBPATH in ascii_uppercase:
           if os.path.exists('%s:\\File.ID' % SVPATH):
              USBPATH='%s:\\' % USBPATH
              print('USB mounted to', USBPATH)
              return USBPATH + ""
      return ""
  
  drive = FETCH_USBPATH()
  while drive == "":
      print('Please plug in USB drive and press any key to continue...', end="")
      input()
      drive = FETCH_USBPATH()
  </code></pre>
  
  <p>This script prompts the user to plug in a drive containing 'file.id' and when attached, prints the drive letter to console and allows the use of 'drive' as a variable.</p>