我想要类似于此 powershell 行“$u=(Get-WmiObject Win32_Volume |Where-Object {$_.Label -eq 'TOSHIBA'}).DriveLetter”的内容,但在 python 中。
我还没有尝试过任何东西,但是这个powershell行可以工作,我想要在python中类似的东西。
安装 pywin32 和 WMI(Windows Management Instrumentation)库以访问 API。
import wmi
# Create a WMI (Windows Management Instrumentation) object
f = wmi.WMI()
# Query Win32_Volume and filter by Label 'TOSHIBA'
volumes = f.Win32_Volume(Label="TOSHIBA")
# Iterate through the filtered volumes
for volume in volumes:
# Get the DriveLetter attribute of the volume
drive_letter = volume.DriveLetter
print(f"Drive Letter: {drive_letter}")
break