Python os.tempnam()方法

Python的tempnam()方法返回创建临时文件的唯一路径名。

语法

以下是tempnam()方法的语法 -

os.tempnam(dir, prefix)

参数

  • dir - 这是将创建临时文件名的目录。
  • prefix - 这是生成的临时文件名的前缀。

返回值

此方法返回唯一路径。

示例

以下示例显示了tempnam()方法的用法。

# !/usr/bin/python3
import os, sys

# prefix is tuts1 of the generated file
tmpfn = os.tempnam('/tmp/tutorialsdir,'tuts1')

print ()"This is the unique path:")
print (tmpfn)

当运行上述程序时,它将在/tmp目录中创建一个符号链接,如下所示: -

This is the unique path:
/tmp/tutorialsdir/tuts1IbAco8

上一篇: Python os模块方法 下一篇: Python异常处理