Is it possible to get writing access to raw devices using python with windows?

前端 未结 2 800
暖寄归人
暖寄归人 2020-12-01 14:39

This is sort of a follow-up to this question. I want to know if you can access raw devices (i.e. \\\\.\\PhysicalDriveN) in writing mode and if this should be th

相关标签:
2条回答
  • 2020-12-01 15:16

    Possibly in Win 7 you have to do something more extreme, such as locking the volume(s) for the disk beforehand with DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, ...)

    In Win 7 you don't have to do that; opening and writing with 'rb+' mode works fine.

    0 讨论(0)
  • 2020-12-01 15:30

    As eryksun and agf pointed out in the comments (but I didn't really get it at first), the solution is rather simple: you have to open the device in the rb+ mode, which opens the device for updating (as I have found out now..) without trying to replace it with a new file (which wouldn't work because the file is in fact a physical drive).

    When writing, you have to write always a whole sector at a time (i.e. multiples of 512-byte), otherwise it fails.

    In addition, the .seek() command can also jump only sector-wise. If you try to seek a position inside a sector (e.g. position 621), the file object will jump to the beginning of the sector where your requested position is (i.e. to the beginning of the second sector, byte 512).

    0 讨论(0)
提交回复
热议问题