shutil 复制命令写入 0 字节文件复制临时文件

shutil copy command writes 0 byte file copying temp files

我有另一种方法调用此函数 3 次以写入 3 个不同的文件。前 2 个文件按预期复制到目标文件夹,第三个文件始终为零字节。如果我关闭删除,我可以看到所有 3 个临时文件都已成功写入。代码没有报告错误,但在过程结束时第三个文件始终为空。

有谁知道为什么 3 次中有 1 次失败?

def write_file(file_destination: str, content: bytes):
    with tempfile.NamedTemporaryFile() as fp:
        fp.write(content)
        shutil.copy(fp.name, file_destination)

以下变体有效,但是,我想了解为什么前两个文件有效而第三个文件在上述代码中无效。

def write_file(file_destination: str, content: bytes):
    with tempfile.NamedTemporaryFile(delete=False) as fp:
        fp.write(content)
    shutil.copy(fp.name, file_destination)
    os.remove(fp.name)

这是因为在执行复制时content还没有写入磁盘。发生这种情况是因为写入被缓冲,并不总是在您调用 file.write 后立即发生。 为确保内容在给定点写入磁盘,您可以使用 file.flush.

对于您的情况,将您的代码更改为:

def write_file(file_destination: str, content: bytes):
    with tempfile.NamedTemporaryFile() as fp:
        fp.write(content)
        fp.flush()
        shutil.copy(fp.name, file_destination)

有关内容何时实际写入磁盘的更多信息,请参阅 io.BufferedWriter 的文档。相关部分是:

The buffer will be written out to the underlying RawIOBase object under various conditions, including:

  • when the buffer gets too small for all pending data;
  • when flush() is called;
  • when a seek() is requested (for BufferedRandom objects);
  • when the BufferedWriter object is closed or destroyed.

因此,在您的第一个示例中,它可能只在某些时候起作用,因为那些时候您正在写入的内容超出了缓冲区,因此需要立即写出。

你的第二个例子,相反,工作,因为当你退出 with 块时,文件被关闭,因此缓冲区需要被刷新并写入磁盘。