{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b5812ce8",
   "metadata": {},
   "source": [
    "# 15-03 · Создание новых файлов\n",
    "\n",
    "Практика к разделу [«Создание новых файлов»](../../site/chapters/glava-15/15-03-sozdanie-fajlov.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50620446",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить режимы записи (w), дозаписи (a) и pathlib."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c4876e3",
   "metadata": {},
   "source": [
    "## Рабочий пример — запись"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f9be995d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.801532Z",
     "iopub.status.busy": "2026-08-18T15:52:49.801391Z",
     "iopub.status.idle": "2026-08-18T15:52:49.828591Z",
     "shell.execute_reply": "2026-08-18T15:52:49.828103Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Уровень 1: 100 очков\n",
      "Уровень 2: 250 очков\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"w\", encoding=\"utf-8\") as file:\n",
    "    file.write(\"Уровень 1: 100 очков\\n\")\n",
    "    file.write(\"Уровень 2: 250 очков\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    print(file.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a610a249",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — дозапись"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c1170511",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.829577Z",
     "iopub.status.busy": "2026-08-18T15:52:49.829468Z",
     "iopub.status.idle": "2026-08-18T15:52:49.832113Z",
     "shell.execute_reply": "2026-08-18T15:52:49.831707Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Уровень 1: 100 очков\n",
      "Уровень 2: 250 очков\n",
      "Уровень 3: 400 очков\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"a\", encoding=\"utf-8\") as file:\n",
    "    file.write(\"Уровень 3: 400 очков\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    print(file.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "675331ac",
   "metadata": {},
   "source": [
    "## Эксперимент 2 — режим w стирает старое содержимое"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7dd519cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.833071Z",
     "iopub.status.busy": "2026-08-18T15:52:49.832964Z",
     "iopub.status.idle": "2026-08-18T15:52:49.837777Z",
     "shell.execute_reply": "2026-08-18T15:52:49.837329Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Новая игра началась.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"rezultaty.txt\", \"w\", encoding=\"utf-8\") as file:\n",
    "    file.write(\"Новая игра началась.\\n\")\n",
    "\n",
    "with open(\"rezultaty.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    print(file.read())   # старые результаты исчезли!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc5e818f",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика — pathlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1df18781",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.838925Z",
     "iopub.status.busy": "2026-08-18T15:52:49.838821Z",
     "iopub.status.idle": "2026-08-18T15:52:49.841533Z",
     "shell.execute_reply": "2026-08-18T15:52:49.841087Z"
    }
   },
   "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\", encoding=\"utf-8\") 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
}
