获取临时 pathlib.Path 的快速方法。
项目描述
临时路径
pathlib.Path
提供一种在系统定义的临时空间中 获取文件的快捷方式。temppath
不wrap ,因为它tempfile.NamedTemporaryFile
在关闭时自动删除,并且不允许在 Windows中写入、关闭然后读取相同的文件,这与 Unix 实现不一致。由于temppath
提供了pathlib.Path
对象,因此这不是问题。
用法
有一个不错的上下文管理器,它将为您删除路径。
from temppath import TemporaryPathContext
with TemporaryPathContext() as t:
t.write_text('the quick brown fox jumps over the lazy dog')
...
do_something_awesome_that_reads(t)
# The file is removed when you leave the `with` context.
如果您需要更多控制权,您还可以选择自己清理它。
from temppath import TemporaryPath
t = TemporaryPath()
t.write_text('the quick brown fox jumps over the lazy dog')
...
do_something_awesome_that_reads(t)
...
t.unlink()