用0字节填充USB

时间:2017-08-06 19:11:53

标签: python python-2.7 performance usb byte

我正在编写一个程序来填充一个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,更高效,更快速?

1 个答案:

答案 0 :(得分:0)

一种非常混乱的方式是将代码中的一百万份def compute_probability_distribution(sides): print([(sides+1, sides), (sides, sides-1), (sides+2, sides-1)]) 硬编码,然后只需要将其写入磁盘即可。我不确定这是否会大大缩短时间。