首页 > 解决方案 > 如果文件在 Pytest 中不存在,如何断言?

问题描述

我知道如何断言文件是否存在,但不确定如果文件不存在最好的方法是什么。我正在使用pytest。

到目前为止我有这个,但这会断言文件是否存在。我想也许使用!not但不确定标准的方法是什么。有点菜鸟。谢谢!

    path = os.path.join("cs.log")
    assert not path.is_file()   

标签: pythonpytest

解决方案


if not os.path.exists("cs.log"):
    #stuff
    pass

或者,使用assert

assert os.path.exists("cs.log") == False

推荐阅读