{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7526156",
   "metadata": {},
   "source": [
    "# 15-03 · Создание новых файлов\n",
    "\n",
    "Практика к разделу [«Создание новых файлов»](../../site/chapters/glava-15/15-03-sozdanie-fajlov.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59958317",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить режимы записи (w), дозаписи (a) и pathlib."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "930982ee",
   "metadata": {},
   "source": [
    "## Рабочий пример — запись"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ca0f812",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:55.349500Z",
     "iopub.status.busy": "2026-08-14T20:58:55.349388Z",
     "iopub.status.idle": "2026-08-14T20:58:55.369752Z",
     "shell.execute_reply": "2026-08-14T20:58:55.369348Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Уровень 1: 100 очков\n",
      "Уровень 2: 250 очков\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"w\") as file:\n",
    "    file.write(\"Уровень 1: 100 очков\\n\")\n",
    "    file.write(\"Уровень 2: 250 очков\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\") as file:\n",
    "    print(file.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "865e472f",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — дозапись"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1755ed70",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:55.370946Z",
     "iopub.status.busy": "2026-08-14T20:58:55.370835Z",
     "iopub.status.idle": "2026-08-14T20:58:55.373412Z",
     "shell.execute_reply": "2026-08-14T20:58:55.372945Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Уровень 1: 100 очков\n",
      "Уровень 2: 250 очков\n",
      "Уровень 3: 400 очков\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"a\") as file:\n",
    "    file.write(\"Уровень 3: 400 очков\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\") as file:\n",
    "    print(file.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56c2662d",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — режим w стирает старое содержимое"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7106dcc2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:55.374423Z",
     "iopub.status.busy": "2026-08-14T20:58:55.374309Z",
     "iopub.status.idle": "2026-08-14T20:58:55.379218Z",
     "shell.execute_reply": "2026-08-14T20:58:55.378747Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Новая игра началась.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"w\") as file:\n",
    "    file.write(\"Новая игра началась.\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\") as file:\n",
    "    print(file.read())   # старые результаты исчезли!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc0cac1",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика — pathlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a6101407",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:55.380328Z",
     "iopub.status.busy": "2026-08-14T20:58:55.380211Z",
     "iopub.status.idle": "2026-08-14T20:58:55.383058Z",
     "shell.execute_reply": "2026-08-14T20:58:55.382554Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "rezultaty.txt\n",
      ".txt\n",
      "Новая игра началась.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "file_path = Path(\"rezultaty.txt\")\n",
    "print(file_path.exists())\n",
    "print(file_path.name)\n",
    "print(file_path.suffix)\n",
    "\n",
    "with file_path.open(\"r\") as f:\n",
    "    print(f.read())"
   ]
  }
 ],
 "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
}
