Python 将 512 字节的零块写入 /dev/sda 有效,但表现不正常

Python Writing a 512-byte block of zeros to /dev/sda works, but doesn't behave like it is working

我在这里尝试创建一个函数来(最终)支持使用多种规格擦除驱动器。我 运行 遇到的问题是,当我尝试将 ZeroBlock 写入磁盘时,它被写入,但代码的行为就像你失败了一样。我说它起作用的原因是它从我的测试系统中的驱动器中清除了引导扇区

def WipeDisk(Drive, WipeSpec, Passes):
    DiskSize = int(System.HDD[Drive].Size)
    DiskSect = int(System.HDD[Drive].Sectors())
    SectSize = int(System.HDD[Drive].SectSz)
    System.HDD[Drive].Start = time.time()
    if (WipeSpec == "Zero"):
            with open("/dev/zero", "rb") as Zero:
                    ZeroBlock = Zero.read(SectSize)
            Zero.close()
            Pass = 0
            with open(System.HDD[Drive].Device, "wb") as Disk:
                    while (Pass < Passes):
                            Current = 1
                            while (Current < DiskSect):
                                    if (Disk.write(ZeroBlock)):
                                            if (Current %((DiskSect*Passes)/100) == 0):
                                                    (variable updates)
                                            if (Current == DiskSect):
                                                    Pass = (Pass+1)
                                    else:
                                            System.HDD[Drive].Error = 1
                                            Pass = Passes
                                            break
                                    Current = (Current+1)
                    if (Pass == Passes):
                            System.HDD[Drive].Current = Current
                            System.HDD[Drive].Percent = "100"
                            System.HDD[Drive].Complete = 1
                    Disk.close()
    else:
            print("Unknown Wipe Specification: "+WipeSpec)

The documentation 对于 file.write 说:

Write a string to the file. There is no return value.

您似乎错误地假设 write returns 某个值指示写入是否成功。它没有。