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

前端 未结 2 809
暖寄归人
暖寄归人 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: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).

提交回复
热议问题