import tempfile with tempfile.TemporaryFile(mode="w+t") as temp_file: temp_file.write("這是一個臨時檔案。\n") temp_file.seek(0) # 重置讀取位置 print("讀取臨時檔案內容:", temp_file.read()) # 離開 `with` 區塊後,臨時檔案將自動刪除