如何使用 Python 建立一個唯一的臨時檔名?


你可以使用 tempfile 模組建立臨時檔案,這種方式是最安全的。檔案的建立沒有競爭條件。檔案僅可由建立的使用者 ID 讀寫。請注意,mkstemp() 的使用者負責刪除臨時檔案。要建立一個新臨時檔案,請使用以下方法 −

import tempfile
_, temp_file_path = tempfile.mkstemp()
print("File path: " + temp_file_path)

注意,你需手動刪除此檔案,否則可能會遺留垃圾檔案。

更新於: 18-Feb-2020

715 次瀏覽

開啟你的 職業

完成課程獲得證書

開始
廣告
© . All rights reserved.