Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to list all the available Windows' drives?

Tags:

python

windows

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

like image 710
Electrons_Ahoy Avatar asked May 05 '09 23:05

Electrons_Ahoy


People also ask

How do I list all hard drives 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.


2 Answers

import win32api  drives = win32api.GetLogicalDriveStrings() drives = drives.split('\000')[:-1] print drives 

Adapted from: http://www.faqts.com/knowledge_base/view.phtml/aid/4670

like image 128
Ayman Hourieh Avatar answered Sep 20 '22 21:09

Ayman Hourieh


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'] 
like image 44
RichieHindle Avatar answered Sep 20 '22 21:09

RichieHindle