如何检查 python 中是否存在原始(Windows)驱动器?即 "\\.\PhysicalDriveN" 其中 N 在磁盘号中
现在我可以通过打开并立即关闭它来检查原始驱动器是否存在(以管理员身份)。如果出现异常,则原始设备可能不存在,否则存在。我知道这不是很pythonic。有没有更好的办法?
os.access(drive_name, os.F_OK)
总是返回False
。与 相同os.path.exists(drive_name)
。我宁愿只使用 python 标准库。os.stat(drive_name)
也找不到设备。
我的工作代码示例:
drive_name = r"\\.\PhysicalDrive1"
try:
open(drive_name).close()
except FileNotFoundError:
print("The device does not exist")
else:
print("The device exists")