【发布时间】: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?
【问题讨论】:
-
@JoshuaNixon 我还没有实现 Windows,但我会把它留到以后,谢谢
标签: python python-2.7 performance usb byte