{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d879317",
   "metadata": {},
   "source": [
    "# 15-28 · Безопасная работа с файлами\n",
    "\n",
    "Практика к разделу [«Безопасная работа с файлами»](../../site/chapters/glava-15/15-28-bezopasnaya-rabota-s-fajlami.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dd9b3b8",
   "metadata": {},
   "source": [
    "## Рабочий пример — сохранение через временный файл"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f584819e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:10.050427Z",
     "iopub.status.busy": "2026-08-18T15:53:10.050285Z",
     "iopub.status.idle": "2026-08-18T15:53:10.072829Z",
     "shell.execute_reply": "2026-08-18T15:53:10.072301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "{'score': 500}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "def bezopasno_sohranit(path, data):\n",
    "    vremenny = path.with_suffix(path.suffix + \".tmp\")\n",
    "    with vremenny.open(\"w\", encoding=\"utf-8\") as f:\n",
    "        json.dump(data, f, ensure_ascii=False, indent=2)\n",
    "    vremenny.replace(path)\n",
    "\n",
    "target = Path(\"bezopasno2.json\")\n",
    "bezopasno_sohranit(target, {\"score\": 500})\n",
    "\n",
    "tmp_exists_after = target.with_suffix(target.suffix + \".tmp\").exists()\n",
    "result_data = json.loads(target.read_text(encoding=\"utf-8\"))\n",
    "print(tmp_exists_after)\n",
    "print(result_data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
