Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้างชื่อไฟล์ชั่วคราวที่ไม่ซ้ำกันด้วย Python

ใช้ tempfile.mkstemp() สร้าง temporary file แบบปลอดภ ไม่มี race condition ไฟล์อ่าน/เขียนได้เฉพาะ user ที่สร้าง

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

หมายเหตุ: ต้องลบไฟล์นี้เองหลังใช้งานเสร็จ