Logical Drive Enumeration

From IronPython Cookbook

This example shows how to query the logical drives attached to the computer using System.Management.ManagementObjectSearcher. (From a blog entry by Steve Gilham.)

import clr
clr.AddReference("System.Management")
import System
from System.Management import SelectQuery, ManagementObjectSearcher

def getLogicalDrives():
    logicalDrives = []
    DiskSearch = ManagementObjectSearcher(SelectQuery("Select * from Win32_LogicalDisk"))
    try:
        moDiskCollection = DiskSearch.Get()
        try:
            for mo in moDiskCollection:
                logicalDrives.append(mo["Name"])
                mo.Dispose()
        finally:
            moDiskCollection.Dispose()
    finally:
        DiskSearch.Dispose()
    return logicalDrives
    
if __name__ == "__main__":
    print getLogicalDrives()

Which when run, gives something like:

['C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:', 'N:']

The ManagementObjectSearcher is a powerful object, the trick is knowing which query to provide...


Back to Contents.

TOOLBOX
LANGUAGES