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%修复率
86%置信度
0证据数
2024-06-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
根因分析
在测试中使用 tmpdir fixture 但未先创建文件,直接尝试读取或写入不存在的路径。
English
在测试中使用 tmpdir fixture 但未先创建文件,直接尝试读取或写入不存在的路径。
解决方案
-
95% 成功率 使用 tmpdir.join 自动创建
file_path = tmpdir.join('file.txt') file_path.write('content') -
85% 成功率 手动创建目录
import os os.makedirs(tmpdir, exist_ok=True) with open(os.path.join(tmpdir, 'file.txt'), 'w') as f: f.write('x')
无效尝试
常见但无效的做法:
-
只创建目录不创建文件
60% 失败
尝试使用 tmpdir.mkdir() 创建目录但忘记创建文件
-
open('subdir/file.txt', 'w')
70% 失败
尝试使用 open() 写入但文件路径包含子目录且未创建