0

I am trying to find the logical or physical offset of the first sector of a file on disk, I have searched a few sites already. Please does anyone know if this is possible in python? See if the code below gives you an idea of what I am trying to do.

with open(r'\\.\C:', 'rb') as disk:
    offset = open(r'C:\path_to_file\file.txt')
    disk.seek(offset)
    disk.tell()
Chex2020
  • 1
  • 2
  • A file doesn't have a single offset. Each block in the file is at a different place on the disk. – Barmar Mar 09 '21 at 00:02
  • Files are not necessarily stored contiguously. – Barmar Mar 09 '21 at 00:03
  • That's true. I just want the offset at the start of the file. – Chex2020 Mar 09 '21 at 00:03
  • It could move between the time you get the offset and seek to it. – Barmar Mar 09 '21 at 00:04
  • What problem are you trying to solve that requires knowing the physical disk location? – Barmar Mar 09 '21 at 00:05
  • 1
    Does this answer your question? [seek() function?](https://stackoverflow.com/questions/11696472/seek-function) – VirtualScooter Mar 09 '21 at 00:05
  • I doubt there's any built-in Python function for this. You need to read the raw directory data, use that to find the inode structure, and then find the disk offset in that. This will be very OS and filesystem-specific. – Barmar Mar 09 '21 at 00:07
  • Its part of a research I am conducting, its a little long and probably too boring to discuss here, but thanks for your suggestions. – Chex2020 Mar 09 '21 at 00:14
  • I am able to find the offset manually using WinHex, I just thought I could automate the process using python. – Chex2020 Mar 09 '21 at 00:16

0 Answers0