Created
November 4, 2014 20:23
-
-
Save nikola/c230313a3bda0e3440e1 to your computer and use it in GitHub Desktop.
Enumerate drives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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