【发布时间】:2015-05-19 02:04:04
【问题描述】:
在这里,我正在尝试创建一个函数来(最终)支持使用多种规范擦除驱动器。我遇到的问题是,当我尝试将 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)
【问题讨论】:
-
你能更好地定义“表现得好像失败了”
-
if (Disk.write(ZeroBlock)): 之后的语句从不执行。它直接跳到相关的 else: 设置 System.HDD[Drive].Error = 1 的地方
-
那里有一堆似乎没有必要的代码,而其他东西却不见了。也就是说,您的代码一开始就非常不符合 Python 风格,特别是您还没有理解
with的想法。我什至会在尝试查找其中的任何错误之前清理所有这些内容,您很有可能会在途中发现并修复错误。