gpt4 book ai didi

python - 使用 tempfile 为我所有的临时文件创建一个子目录

转载 作者:太空狗 更新时间:2023-10-30 00:30:25 25 4
gpt4 key购买 nike

我一直在使用带有前缀的 tempfile.mkdtemp 来创建我的临时文件。这导致我的 tmp 文件夹中有很多不同的目录,其中包含“tmp/myprefix{uniq-string}/”。

我想改变这个并有一个子目录,这样我创建的临时文件夹都在一个主目录下,这样前缀实际上是 tmp 的子文件夹 'tmp/myprefix/{uniq-string}/'.

此外,我不想覆盖 tempfile 的系统来定义默认的 tmp 目录。

我尝试使用“prefix”和“dir”参数但没有成功。

最佳答案

要使用 dir 参数,您必须确保 dir 文件夹存在。这样的事情应该有效:

import os
import tempfile

#define the location of 'mytemp' parent folder relative to the system temp
sysTemp = tempfile.gettempdir()
myTemp = os.path.join(sysTemp,'mytemp')

#You must make sure myTemp exists
if not os.path.exists(myTemp):
os.makedirs(myTemp)

#now make your temporary sub folder
tempdir = tempfile.mkdtemp(suffix='foo',prefix='bar',dir=myTemp)

print tempdir

关于python - 使用 tempfile 为我所有的临时文件创建一个子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8856053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com