How to get a list of local drives without SUBST'ituted ones in Python / Windows?
I'm looking for a way to get all the local drives on a Windows machine, So far, I tried with two options
1)
# Win32Com
from win32com.client import Dispatch
import sys
fso = Dispatch('Scripting.FileSystemObject')
for drive in fso.Drives:
print drive, drive.DriveType
2)
# win32api
import win32api
import win32file
drives = (drive for drive in win32api.GetLogicalDriveStrings().split("\000") if drive)
for drive in drives:
print drive, win32file.GetDriveType(drive)
This two ways works (almost) fine, I get my drive list such as:
A: 1 // Removable
C: 2 // Fixed
D: 2
E: 2
G: 2 // Fixed (??? SUBS开发者_运维问答T'ed drive)
I: 4 // Cd-Rom
X: 3 // Network
but the G: drive is a SUBST'ed drive (eg: created with SUBST G: C:\TEST), and I cannot find the way to differentiate it from a "real" local drive.
Any ideas?
TIA, Pablo
Google tells me that if you try and fetch a GUID for a SUBST-ed drive it will fail:
>>> import win32file
>>> win32file.GetVolumeNameForVolumeMountPoint("C:\\")
'\\\\?\\Volume{50c800a9-c62e-11df-b5bb-806e6f6e6963}\\'
>>> win32file.GetVolumeNameForVolumeMountPoint("K:\\")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pywintypes.error: (87, 'GetVolumeNameForVolumeMountPoint',
'The parameter is incorrect.')
This seems to work but may not be reliable.
精彩评论