我一直在尝试在python 3中创建一个方法来检测Windows引导驱动器号(大多数情况下是C :),但是我遇到了麻烦。最初,我使用WMI搜索名为Windows的分区,但是我了解到并非每台计算机都带有该名称的分区(即,对于HP计算机,驱动器被标记为HP)。我还没有想出可以将Windows启动驱动器与其他驱动器区分开的东西。如果有人有一个主意,我全神贯注。我的代码发布在下面。
def getHDD(): #returns the hard drive disk letter. The hard drive should be
named "Windows"
c = wmi.WMI ()
for physical_disk in c.Win32_DiskDrive ():
for partition in physical_disk.associators
("Win32_DiskDriveToDiskPartition"):
for logical_disk in partition.associators
("Win32_LogicalDiskToPartition"):
if ("Windows" == logical_disk.VolumeName):
return logical_disk.Caption