import os
import string
letters = list(string.ascii_lowercase) #Just a list of all the lowercase letters
def list_subtract(list1, list2): #Function to return all entries in list1 subtracted by the entries in list2
list_difference = []
for x in list1:
if x not in list2:
list_difference.append(x)
return list_difference
def devices():
not_drives = []
for x in letters:
try:
drive = os.listdir('%(test_drive)s:' % {'test_drive' : x}) #It tries os.listdir() for all 26 drive letters
except (FileNotFoundError, PermissionError): #If it recieves a FileNotFoundError (Drive doesn't exist) or a PermissionError (Drive is busy)
not_drives.append(x) #Puts that drive letter in a list of all drive letters that don't correspond to a drive
return list_subtract(letters, not_drives) #returns all letters - letters that aren't drives
print(devices())
My program uses the try statement, meaning it will execute the os.listdir(), and if it returns one of thee two errors I specified in the except clause, it will append the drive letter that caused that error to a list, then return the list. I didn't test it much, so if there are any other errors that you receive during testing, you may want to add those to the except. There is probably a more efficient way to do this, but it works, right? Good enough for me.