【问题标题】:Filling a USB with 0 bytes用 0 字节填充 USB
【发布时间】:2018-01-14 02:16:22
【问题描述】:

我正在编写一个程序,它将用 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?

【问题讨论】:

标签: python python-2.7 performance usb byte


【解决方案1】:

一种非常混乱的方法是硬编码,例如,将一百万份\0 复制到代码中,然后只要将其写入磁盘就可以了?我不确定这是否会大大减少时间。

【讨论】:

    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多