如何写入 Python 中的临时文件并再次从中读取?

How to write to a temporary file in Python and read from it again?

我正在尝试为将数据写入 CSV 文件的函数编写单元测试。为此,我想使用 tempfile.NamedTemporaryFile 写入一个命名的临时文件并再次打开它,我从文档中了解到这在 Unix 平台上应该是可能的。

但是,如果我尝试这个测试,

import csv
import tempfile


def write_csv(csvfile):
    writer = csv.DictWriter(csvfile, fieldnames=['foo', 'bar'])

    writer.writeheader()
    writer.writerow({'foo': 1, 'bar': 2})


def test_write_csv():
    with tempfile.NamedTemporaryFile(mode='w') as csvfile:
        write_csv(csvfile)

    with open(csvfile.name) as csvfile:
        reader = csv.DictReader(csvfile)

我得到一个 FileNotFoundError:

> pytest csvtest.py -s
======================================= test session starts ========================================
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/kurtpeek/Documents/Scratch
collected 1 item                                                                                   

csvtest.py F

============================================= FAILURES =============================================
__________________________________________ test_write_csv __________________________________________

    def test_write_csv():
        with tempfile.NamedTemporaryFile(mode='w') as csvfile:
            write_csv(csvfile)

>       with open(csvfile.name) as csvfile:
E       FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/fr/7gjx_3js67xg0pjxz6sptktc0000gn/T/tmpgml8_fwf'

csvtest.py:16: FileNotFoundError
===================================== 1 failed in 0.03 seconds =====================================

我尝试了各种替代方法,例如尝试在写入文件时在同一 with 块中打开文件(在这种情况下,我得到一个错误,它未打开以供读取),在 'rw' 模式(在这种情况下 csv 不允许我写入)。

如何将 CSV 写入临时文件并在单元测试中再次读取?

一旦您离开 withcsvfile 的上下文就会消失,并且临时文件会被删除。你打算这样做吗?

def test_write_csv():
    with tempfile.NamedTemporaryFile(mode='w') as csvfile:
        write_csv(csvfile)

        with open(csvfile.name) as csvfile:
            reader = csv.DictReader(csvfile)

可能不会,因为您重复使用了 csvfile。如果你打算保留文件,你可以通过 delete=false

csvfile = tempfile.NamedTemporaryFile(delete=False)

临时文件关闭后如果想保留,需要通过delete=False。来自您链接的 tempfile.NamedTemporaryFile 的文档:

If delete is true (the default), the file is deleted as soon as it is closed.

然后你只需要做:

import csv
import tempfile

def write_csv(csvfile):
    writer = csv.DictWriter(csvfile, fieldnames=['foo', 'bar'])
    writer.writeheader()
    writer.writerow({'foo': 1, 'bar': 2})

def test_write_csv():
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as csvfile:
        write_csv(csvfile)
    with open(csvfile.name) as csvfile:
        reader = csv.DictReader(csvfile)

with块执行结束时,文件被删除。所以,也许这不是最好的解决方案,但实际上它是有效的,所以你可以试试看:

def test_write_csv():
    with tempfile.NamedTemporaryFile(mode='w') as csvfile:
        write_csv(csvfile)
        with open(csvfile.name) as csvfile:
            reader = csv.DictReader(csvfile)