如何使用Python创建唯一的目录名称?

您可以使用tempfile模块以最安全的方式创建唯一的临时目录。目录的创建中没有竞争条件。该目录仅由创建用户ID可读,可写和可搜索。请注意,使用的用户mkdtemp()负责在完成后删除临时目录。要创建新的临时目录,请按以下方式使用它-

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

请注意,完成此目录后,您需要手动删除它。