Skip to content

Instantly share code, notes, and snippets.

@nikola nikola/drives.py
Created Nov 4, 2014

Embed
What would you like to do?
Enumerate drives
# coding: utf-8
import re
from itertools import izip_longest
import win32com.client
from win32file import GetDriveType
from win32api import GetLogicalDriveStrings, GetVolumeInformation
def _grouper(n, iterable):
args = [iter(iterable)] * n
return izip_longest(fillvalue=None, *args)
def getLongPathname(pathname):
if pathname.startswith('\\\\'):
return pathname.replace(u'\\\\', u'\\\\?\\UNC\\')
elif re.search('^[a-z]:\\\\', pathname, re.I) is not None:
return u'\\\\?\\' + pathname
else:
return pathname
if __name__ == '__main__':
detectedDrives = []
networkPathsByLetter = dict(_grouper(2, win32com.client.Dispatch('WScript.Network').EnumNetworkDrives()))
if '' in networkPathsByLetter:
del networkPathsByLetter['']
for driveLetter in GetLogicalDriveStrings().split('\000'):
if driveLetter:
driveType = GetDriveType(driveLetter)
if driveType == 3:
detectedDrives.append({
'drive': driveLetter[0],
'label': GetVolumeInformation(driveLetter)[0] or 'Local disk',
'pathname': getLongPathname(driveLetter + '\\'),
})
elif driveType == 4:
detectedDrives.append({
'drive': driveLetter[0],
'label': networkPathsByLetter[driveLetter[:2]],
'pathname': getLongPathname(networkPathsByLetter[driveLetter[:2]]),
})
print detectedDrives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.