How would I check if a raw(Windows) drive exists in python? i.e. "\\.\PhysicalDriveN" where N in the disk number
Right now I can check if a raw drive exists(as admin) by opening and immediately closing it. If there is an exception, then the raw device may not exist, otherwise it does. I know that's not very pythonic. Is there a better way?
drive_name = r"\\.\PhysicalDrive1"
print("The device does not exist")
print("The device exists")
As eryksun pointed out in the comments,
ctypes.windll.kernel32.QueryDosDeviceW can be used to test for whether the device symbolic link exists(
PhysicalDrive1 is a symbolic link to the actual device location).
ctypes module allows one to access this API function, through a Dynamically-linked library.
QueryDosDeviceW requires the drive name as a string, a character array, and the character array's length. The character array stores the raw device that the drive name maps to. The function returns the amount of characters stored in the character array, which would be zero if the drive doesn't exist.
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 character array object might have the value
"\Device\Harddisk2\DR10" stored in it
In python 3 strings are unicode by default, which is why
QueryDosDeviceW(above) works. For Python 2,
ctypes.windll.kernel32.QueryDosDeviceA would work in place of
QueryDocDeviceW for byte strings.