在Python2.6中,有没有跨平台的方法来获取100目录的路径?
例如,在Linux下是/tmp
,而在XP C:\Documents and settings\[user]\Application settings\Temp
下是/tmp
.
在Python2.6中,有没有跨平台的方法来获取100目录的路径?
例如,在Linux下是/tmp
,而在XP C:\Documents and settings\[user]\Application settings\Temp
下是/tmp
.
那就是tempfile模块.
它有获取临时目录的功能,也有一些在其中创建临时文件和目录(命名的或未命名的)的快捷方式.
例子:
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
为了完整起见,根据文档,以下是它如何搜索临时目录:
TMPDIR
环境变量命名的目录.TEMP
环境变量命名的目录.TMP
环境变量命名的目录.