如何找到可用 CD/DVD 驱动器的盘符?
我在 Windows 上使用 Python 2.5.4。
最佳答案
使用 win32api 你可以获得驱动器列表并使用 GetDriveType 你可以检查它是什么类型的驱动器,你可以通过 'Python for Windows Extensions' 访问 win32api或 ctypes 模块
这是一个使用 ctypes 的例子:
import string
from ctypes import windll
driveTypes = ['DRIVE_UNKNOWN', 'DRIVE_NO_ROOT_DIR', 'DRIVE_REMOVABLE', 'DRIVE_FIXED', 'DRIVE_REMOTE', 'DRIVE_CDROM', 'DRIVE_RAMDISK']
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
for drive in get_drives():
if not drive: continue
print "Drive:", drive
try:
typeIndex = windll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
print "Type:",driveTypes[typeIndex]
except Exception,e:
print "error:",e
这个输出:
Drive: C
Type: DRIVE_FIXED
Drive: D
Type: DRIVE_FIXED
Drive: E
Type: DRIVE_CDROM
关于python - 使用 Python (Windows) 枚举 CD 驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2288065/