最省心的是 tempfile.NamedTemporaryFile,默认 with 退出自动删除;Windows 需设 delete=False 并手动 os.unlink;跨平台安全写法即此;mkstemp+atexit 适合进程退出清理但异常崩溃不保证;TemporaryDirectory 适用于多文件临时目录而非单文件。

用 tempfile.NamedTemporaryFile 最省心
它默认在 with 块退出时自动删除文件,前提是没显式关闭或设 delete=False。注意:Windows 下必须设 delete=False 才能正常读写(因文件锁机制),但这时需手动清理。
- Linux/macOS 可直接用:
with tempfile.NamedTemporaryFile(delete=True) as f: f.write(b"hello") f.seek(0) print(f.read()) # 文件存在且可读 # 退出 with 后文件已删 - 跨平台安全写法(尤其 Windows):
with tempfile.NamedTemporaryFile(delete=False) as f: fname = f.name f.write(b"hello") # 必须手动删 os.unlink(fname) - 别在
with内调f.close(),否则后续操作可能报ValueError: I/O operation on closed file
想保留路径但延迟删除?用 tempfile.mkstemp + atexit
mkstemp 返回文件描述符和路径,不自动管理生命周期。若想“退出进程时删”,配合 atexit.register 是轻量方案,但要注意:异常崩溃、os._exit 或 SIGKILL 会绕过清理。
- 典型用法:
fd, path = tempfile.mkstemp(suffix=".log") atexit.register(os.unlink, path) # 后续用 os.fdopen(fd, "w") 包装成文件对象
- 不要用
with os.fdopen(fd, ...)后再依赖atexit——fdopen关闭时会关掉 fd,atexit删文件仍成功,但 fd 已失效 - 多线程不安全:多个线程注册同一路径的
atexit不会去重,可能重复删报错
为什么不用 tempfile.TemporaryDirectory?
它管目录,不是文件。如果你只是写一个临时文件,用它就过度了;但若需要临时建一堆文件/子目录,它比手管路径+shutil.rmtree 更可靠——with 退出时递归删整个目录,且支持 ignore_errors=True 容忍只读文件。
- 适用场景:测试中生成 config + data + cache 多个临时文件
- 不适用场景:单个需频繁读写的临时文件(打开路径麻烦,且目录级清理太重)
- 注意:它的
name是目录路径,不是文件路径,别误当文件用
容易被忽略的坑:权限与可见性
所有 tempfile 模块函数默认创建的文件权限是 0o600(仅属主读写),在某些容器或 CI 环境里,若后续程序以不同用户运行,会因权限拒绝访问;另外,NamedTemporaryFile 在 Linux 上创建的是“无名 inode”,即使路径还在,ls 也看不到,只有 lsof 能查到——这常让人误以为文件没删干净。
- 需放宽权限?传
mode=0o644(但注意安全边界) - 调试时想确认文件是否真存在:用
os.path.exists(path),别信ls - 在 Docker 中挂载了
/tmp卷?临时文件可能落在宿主机上,with结束后删的是容器内路径,宿主机文件残留










