3

I am using Python to open the physical disk in the computer to read the first sector.

disk = r"\\.\PhysicalDrive0"
with open(disk, 'r') as f:
        f.seek(0)
        partdata = f.read(512)
len(partdata)

In windows XP len(partdata) will return 512 and have the correct content. In windows 7 len(partdata) returns 230 and the data is correct until it breaks off.

Also, trying to seek farther into the disk doesn't work and it only returns the first 230 bytes.

Python versions 2.7.1 and 2.7.2 32bit and 64bit

On Windows 7, one 32bit machine one 64bit.

Shawn
  • 75
  • 5
  • Interesting problem. I presume you're running this code as admin? I'd imagine if there were a permissions factor here you'd either get all the data or none of it.. Anyway, neat problem. – James T Snell Jun 22 '11 at 23:36
  • Yes, it is run as Admin on Win7. If you don't you can't open the file at all. – Shawn Jun 22 '11 at 23:38

1 Answers1

7

Try opening in binary mode

with open(disk, 'rb') as f:
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Thank you. This was the problem. Any idea why XP worked and 7 doesn't? – Shawn Jun 22 '11 at 23:42
  • 1
    @Shawn, presumably the data you were reading didn't have any of the bytes that windows treats specially in text mode. In Particular chr(26) would mean "End Of File" in text mode – John La Rooy Jun 23 '11 at 00:33