普贤教程 › 包参考
tempfile 包
临时文件/目录
对照 Python tempfile / Go ioutil.TempFile 心智:
| 项 | 值 |
|---|---|
| 包名 | tempfile |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | tempfile.px |
| 分类 | 文件系统 |
| 文件数 / 行数 | 1 / 91 |
安装与引入
bash
pxpkg init # 首次 pxpkg add tempfile@0.2.0 pxpkg install
px
import tempfile所属分类教程:文件系统
公开接口
| 函数 | 参数 |
|---|---|
tf_tempdir | prefix |
tf_tempname | prefix |
tf_write | path, content |
tf_read | path |
tf_remove_file | path |
tf_remove_dir | dir |
其余 6 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
o_seq | |
tf_tempname_ext | prefix, ext |
tf_create | prefix, content |
tf_with_tempfile | prefix, content, callback |
tf_cleanup | paths |
tf_mkdir | prefix |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/tempfile —— 临时文件/目录 ------------------------------------------------------------ 对照 Python tempfile / Go ioutil.TempFile 心智: tf_tempdir(prefix?) → Ok(str) 在 /tmp 建唯一临时目录 tf_tempname(prefix?) → Ok(str) 唯一临时文件路径(不建文件) tf_write(path, content) → Ok 写文件(str 或 bytes 内容) tf_read(path) → Ok(str) 读文件(read_file 原样) tf_remove_file(path) → Ok 删除单个文件 tf_remove_dir(dir) → Ok 删除空目录 说明: - 命名基于 now_ms(同一毫秒冲突概率可忽略;prefix 可自定义)。 - 无删除目录树原语(remove 非空目录会响亮报错),tf_remove_dir 仅用于空目录; 用完临时目录建议先 tf_remove_file 各文件再 tf_remove_dir。 ============================================================