demo_tempfile.py 283 B

123456789101112
  1. import tempfile
  2. import os
  3. # 创建临时目录
  4. temp_dir = tempfile.mkdtemp(prefix="A")
  5. # 在临时目录中创建文件
  6. file_path = os.path.join(temp_dir, "my_file.txt")
  7. with open(file_path, 'w') as file:
  8. file.write("This is a file in a temporary directory.")
  9. print(file_path)