python
runtime_error
ai_generated
true
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pytest-of-user/pytest-0/test_foo0/somefile.txt'
ID: python/pytest-tmpdir-file-not-created
80%Fix Rate
86%Confidence
0Evidence
2024-06-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
Root Cause
在测试中使用 tmpdir fixture 但未先创建文件,直接尝试读取或写入不存在的路径。
generic中文
在测试中使用 tmpdir fixture 但未先创建文件,直接尝试读取或写入不存在的路径。
Workarounds
-
95% success 使用 tmpdir.join 自动创建
file_path = tmpdir.join('file.txt') file_path.write('content') -
85% success 手动创建目录
import os os.makedirs(tmpdir, exist_ok=True) with open(os.path.join(tmpdir, 'file.txt'), 'w') as f: f.write('x')
Dead Ends
Common approaches that don't work:
-
只创建目录不创建文件
60% fail
尝试使用 tmpdir.mkdir() 创建目录但忘记创建文件
-
open('subdir/file.txt', 'w')
70% fail
尝试使用 open() 写入但文件路径包含子目录且未创建