Python os.tempnam()方法

2019-10-16 23:05:14

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