简体   繁体   English

pytest 中的 tmpdir 是否仅在我们写入文件时才创建文件?

[英]Does tmpdir in pytest create a file only when we write to it?

When I use fp = tmpdir.join("hello.txt") in pytest, does pytest actually create a file in my temp directory or it creates only when I write fp.write("hello") ?当我在 pytest 中使用fp = tmpdir.join("hello.txt")时,pytest 是否实际上在我的临时目录中创建了一个文件,或者它仅在我编写fp.write("hello")时创建?

No, pytest will not create the file until you actually write to it.不, pytest在您实际写入文件之前不会创建该文件。 Under the hood, as mentioned in the documentation, tmpdir uses py.path .在幕后,如文档中所述, tmpdir使用py.path When you call join it will lazily get the new path, meaning it does not exist until you actually write something to it.当您调用join时,它会懒惰地获取新路径,这意味着它不存在,直到您实际向其写入内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM