首页 > 解决方案 > 如何找到硬盘的最后一个扇区?

问题描述

我没有尝试通过将扇区数乘以 10 来找到最后一个扇区。我试图通过增加 1 来找到它,但是系统非常疲倦并且花费了很多时间。我不想过滤掉就绪命令的输出。

如何找到柱面、磁头和扇区的数量。我想我会通过从chs系统转换为lba系统来获得扇区数。

import os
def main():
    s=1
    if os.name == "nt":
        while True:
            if read_sector(r"\\.\physicaldrive0",s)=='':
                break
            else:
                s=s*10
            print(s)
    else:
        while True:
            if read_sector("/dev/sda",s)=='':
                break
            else:
                s=s*10
            print(s)
def read_sector(disk, sector_no=0):
    f = open(disk, 'rb')
    f.seek(sector_no * 1)
    read = f.read(1)
    return read

if __name__ == "__main__":
    main()

或者

import os
def main():
    s=0
    if os.name == "nt":
        while True:
            if read_sector(r"\\.\physicaldrive0",s)=='':
                break
            else:
                s=s+1
            print(s)
    else:
        while True:
            if read_sector("/dev/sda",s)=='':
                break
            else:
                s=s+1
            print(s)
def read_sector(disk, sector_no=0):
    f = open(disk, 'rb')
    f.seek(sector_no * 1)
    read = f.read(1)
    return read

if __name__ == "__main__":
    main()

标签: pythonpython-3.xfile

解决方案


import os
def end_sector(disk):
    os.system("fdisk -l %s >fdisk.lst"%disk)
    with open("fdisk.lst") as file:
        dosya=file.read()
        dosya=dosya.split()
        j=0
        for i in dosya:
            j=j+1
        for i in range(j):
            if dosya[i]=="sektör":
                max_sector=int(dosya[i-1])
        for i in range(j):
            if dosya[i]=="=" and dosya[i-1]==dosya[i+1]:
                sector_size=int(dosya[i+1])
    return max_sector,sector_size
dizi=end_sector("/dev/sdb")
print(dizi)

推荐阅读