如何检查 Python 中是否存在原始(未安装)Windows 驱动器
How to check if a Raw(unmounted) Windows Drive exists in Python
如何检查 python 中是否存在原始 (Windows) 驱动器?即“\\.\PhysicalDriveN”,其中磁盘编号中的 N
现在我可以通过打开并立即关闭它来检查原始驱动器是否存在(以管理员身份)。如果有异常,则原始设备可能不存在,否则存在。我知道这不是很 pythonic。有没有更好的方法?
os.access(drive_name, os.F_OK)
总是 returns 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")
正如eryksun在评论中指出的那样,ctypes.windll.kernel32.QueryDosDeviceW
可用于测试设备符号link是否存在(PhysicalDrive1
是一个符号link 到实际的设备位置)。
ctypes
模块允许通过动态linked 库访问这个API 函数。
QueryDosDeviceW
需要字符串形式的驱动器名称、字符数组和字符数组的长度。字符数组存储驱动器名称映射到的原始设备。函数returns存储在字符数组中的字符数量,如果驱动器不存在则为零。
import ctypes
drive_name = "PhysicalDrive1"
target = (ctypes.c_wchar * 32768)(); # Creates an instance of a character array
target_len = ctypes.windll.kernel32.QueryDosDeviceW(drive_name, target, len(target))
if not target_len:
print("The device does not exist")
else:
print("The device exists")
target
字符数组对象可能存储了值 "\Device\Harddisk2\DR10"
备注
在 python 中,3 个字符串默认为 unicode,这就是 QueryDosDeviceW
(above) 有效的原因。对于 Python 2,ctypes.windll.kernel32.QueryDosDeviceA
可以代替字节字符串的 QueryDocDeviceW
。
无需导入 ctypes 等
os.path.exists("C:")
工作正常。驱动程序参数应该有一个尾随的“:”字符。
>>> os.path.exists("C:")
True
>>> os.path.exists("D:")
True
>>> os.path.exists("A:")
False
>>> os.path.exists("X:")
True # i have mounted a local directory here
>>> os.path.exists("C")
False # without trailing ":"
如何检查 python 中是否存在原始 (Windows) 驱动器?即“\\.\PhysicalDriveN”,其中磁盘编号中的 N
现在我可以通过打开并立即关闭它来检查原始驱动器是否存在(以管理员身份)。如果有异常,则原始设备可能不存在,否则存在。我知道这不是很 pythonic。有没有更好的方法?
os.access(drive_name, os.F_OK)
总是 returns 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")
正如eryksun在评论中指出的那样,ctypes.windll.kernel32.QueryDosDeviceW
可用于测试设备符号link是否存在(PhysicalDrive1
是一个符号link 到实际的设备位置)。
ctypes
模块允许通过动态linked 库访问这个API 函数。
QueryDosDeviceW
需要字符串形式的驱动器名称、字符数组和字符数组的长度。字符数组存储驱动器名称映射到的原始设备。函数returns存储在字符数组中的字符数量,如果驱动器不存在则为零。
import ctypes
drive_name = "PhysicalDrive1"
target = (ctypes.c_wchar * 32768)(); # Creates an instance of a character array
target_len = ctypes.windll.kernel32.QueryDosDeviceW(drive_name, target, len(target))
if not target_len:
print("The device does not exist")
else:
print("The device exists")
target
字符数组对象可能存储了值 "\Device\Harddisk2\DR10"
备注
在 python 中,3 个字符串默认为 unicode,这就是 QueryDosDeviceW
(above) 有效的原因。对于 Python 2,ctypes.windll.kernel32.QueryDosDeviceA
可以代替字节字符串的 QueryDocDeviceW
。
无需导入 ctypes 等
os.path.exists("C:")
工作正常。驱动程序参数应该有一个尾随的“:”字符。
>>> os.path.exists("C:")
True
>>> os.path.exists("D:")
True
>>> os.path.exists("A:")
False
>>> os.path.exists("X:")
True # i have mounted a local directory here
>>> os.path.exists("C")
False # without trailing ":"