我知道如何断言文件是否存在,但不确定如果文件不存在,最好的方法是什么。我正在使用 pytest。
到目前为止我有这个,但是如果文件确实存在,它会断言。我想也许使用 !
或 not
但不确定标准的做法是什么。有点菜鸟。谢谢!
path = os.path.join("cs.log")
assert not path.is_file()
答案 0 :(得分:3)
if not os.path.exists("cs.log"):
#stuff
pass
或者,使用 assert
:
assert os.path.exists("cs.log") == False