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.
Currently I have:
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'):
-- 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?).
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
print('Please plug in our USB drive')
return-to-start
Kind of like a GOTO command-prompt command?
EDIT:
For people with similar future inquiries, here's the solution:
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()
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.