【问题标题】:How to get a list of local drives without SUBST'ituted ones in Python / Windows?如何在 Python / Windows 中获取没有 SUBSTituted 的本地驱动器列表?
【发布时间】:2010-11-11 18:25:50
【问题描述】:

我正在寻找一种方法来获取 Windows 机器上的所有本地驱动器, 到目前为止,我尝试了两种选择

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)

这两种方式(几乎)工作正常,我得到我的驱动器列表,例如:

A: 1    // Removable
C: 2    // Fixed
D: 2    
E: 2
G: 2    // Fixed    (??? SUBST'ed drive)
I: 4    // Cd-Rom
X: 3    // Network

但 G: 驱动器是 SUBST 驱动器(例如:使用 SUBST G: C:\TEST 创建), 而且我找不到将它与“真正的”本地驱动器区分开来的方法。

有什么想法吗?

TIA, 巴勃罗

【问题讨论】:

    标签: python windows-xp


    【解决方案1】:

    Google 告诉我,如果您尝试获取 SUBST 驱动器的 GUID,它将失败:

    >>> 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.')
    

    这似乎可行,但可能不可靠。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2014-09-24
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多