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?
os.access(drive_name, os.F_OK)
always returns False
. Same with with os.path.exists(drive_name)
. I'd prefer just to use the python standard library. os.stat(drive_name)
cannot find the device either.
Example of my working code:
drive_name = r"\\.\PhysicalDrive1"
try:
open(drive_name).close()
except FileNotFoundError:
print("The device does not exist")
else:
print("The device exists")