2017-08-06 82 views
2

我正在编写一个程序,它将填充0字节的USB驱动器,以便将其转换为可启动磁盘(该部分将在稍后提供)。用0字节填充USB

现在我已经是这样的:

import io 
import lib 


class CancelBlockCipherException(Exception): pass 


class Formatter(object): 

    def __init__(self, usb): 
     self.usb = usb 

    def _erase_all(self, block_cipher): 
     with io.FileIO(self.usb, "w") as _usb: 
      while _usb.write(block_cipher): pass 

    def format_usb(self, size, default="y"): 
     block_cipher = b"\0" * size 
     confirmation = lib.settings.prompt(
      "Your USB is about to be completely erased, everything on this " 
      "drive will be gone, are you sure you want to continue", "y/N(default '{}')".format(default) 
     ) 
     if confirmation.lower().startswith("y") or confirmation == "": 
      lib.settings.LOGGER.warning("Erasing USB content..") 
      self._erase_all(block_cipher) 
     elif confirmation.lower() == "" or confirmation is None: 
      lib.settings.LOGGER.warning("Erasing USB content..") 
      self._erase_all(block_cipher) 
     else: 
      raise CancelBlockCipherException("User aborted block cipher.") 

这将完成的是采取以字节为USB的大小,并写\0/dev/sdb1,直到它完全以0字节填充USB文件(至少这是我认为正在发生的事情,之前我错了)。我想要做的就是更快地写入文件。目前这可能需要10-15分钟才能完成,因为编写\0 1,875,615,744(1.75GB)时间可能会占用大量时间。如何使用纯python成功地更高效,更快速地格式化此USB?

+0

不知道这是你在找什么https://stackoverflow.com/questions/27568706/format-drive-in-python –

+0

@JoshuaNixon我还没有实现Windows,但我将它保存为以后,谢谢 – mawi

回答

0

一个非常混乱的方法是将代码中的一百万份\0硬编码到硬盘中,并且只要将它写入磁盘的时间就够长了。我不确定这是否会大大缩短时间。