【发布时间】:2021-12-03 22:49:42
【问题描述】:
我想打开并写入 python 的tempfile.TemporaryDirectory() 中的文件
这是我目前的代码:
import tempfile
temp_dir = tempfile.TemporaryDirectory()
destination = temp_dir.name
file = 'concept_aliases.json'
spath = os.path.join(destination,"/",file)
with open(spath, "w+") as f::
# do smthg
但是,我收到此错误:
PermissionError: [Errno 13] Permission denied: '/concept_aliases.json'
【问题讨论】:
-
这可能和stackoverflow.com/questions/23212435/…是同一个问题
-
去掉
"/"参数到os.path.join()- 该函数的全部意义在于它为您提供了适当的路径名分隔符。这里发生的是斜杠被解释为绝对路径名(到根目录);之前的所有内容都会被丢弃,并且您最终会引用根目录中的文件in(您没有写入权限)。