Dùng module tempfile tạo thư mục tạm duy nhất an toàn nhất. Không có race condition khi tạo. Thư mục chỉ readable/writable/searchable bởi user tạo. Lưu ý người dùng mkdtemp() tự xóa thư mục tạm khi xong. Tạo thư mục tạm mới:
import tempfile _, temp_dir_path = tempfile.mkdtemp() # Làm gì đó với thư mục này # Xóa thư mục khi xong
Lưu ý: phải tự xóa thư mục này sau khi xong.