Is there a way in Python to list all the currently in-use drive letters in a Windows system?
(My Google-fu seems to have let me down on this one)
A C++
equivalent: Enumerating all available drive letters in Windows
Right-click on "Command Prompt" and choose "Run as Administrator". At the prompt, type "diskpart" and hit Enter. At the diskpart prompt type "list disk". This will list all the hard drives in the system.
import win32api drives = win32api.GetLogicalDriveStrings() drives = drives.split('\000')[:-1] print drives
Adapted from: http://www.faqts.com/knowledge_base/view.phtml/aid/4670
Without using any external libraries, if that matters to you:
import string from ctypes import windll def get_drives(): drives = [] bitmask = windll.kernel32.GetLogicalDrives() for letter in string.uppercase: if bitmask & 1: drives.append(letter) bitmask >>= 1 return drives if __name__ == '__main__': print get_drives() # On my PC, this prints ['A', 'C', 'D', 'F', 'H']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With