import tempfile
print tempfile.gettempdir() # prints the current temporary directory
f = tempfile.TemporaryFile()
f.write('something on temporaryfile')
f.seek(0) # return to beginning of file
print f.read() # reads data back from the file
f.close() # temporary file is automatically deleted here
那就是tempfile模块。
它有获取临时目录的功能,还有一些快捷方式来创建临时文件和目录,可以是命名的,也可以是未命名的。
示例:
为了完整起见,下面是根据文档搜索临时目录的方法:
TMPDIR
环境变量命名的目录。TEMP
环境变量命名的目录。TMP
环境变量命名的目录。Wimp$ScrapDir
环境变量命名的目录。C:\TEMP
、C:\TMP
、\TEMP
和\TMP
按顺序排列。/tmp
、/var/tmp
和/usr/tmp
按此顺序排列。这应该符合您的要求:
在我的窗口框上,我得到:
在我的Linux机器上,我得到:
我使用:
这是因为在MacOS上,即Darwin,
tempfile.gettempdir()
和os.getenv('TMPDIR')
返回一个值,例如'/var/folders/nj/269977hs0_96bttwj2gs_jhhp48z54/T'
;这是我不想要的!相关问题 更多 >
编程相关推荐