如何使用 Python 建立一個唯一的目錄名稱?


您可以使用 tempfile 模組以儘可能安全的方式建立一個唯一的臨時目錄。目錄的建立過程中不存在競爭條件。該目錄只能由建立它的使用者 ID 讀、寫和搜尋。請注意,mkdtemp() 的使用者負責在使用完成後刪除臨時目錄。要建立新臨時目錄,請按以下方法進行:-

import tempfile
_, temp_dir_path = tempfile.mkdtemp()
# Do what you want with this directory
# And remove the directory when done

請注意,使用完成後您需要手動刪除此目錄。

更新於: 2020 年 2 月 18 日

939 次瀏覽

開啟你的 職業生涯

完成課程以獲取認證

開始
廣告
© . All rights reserved.