{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "050a099e",
   "metadata": {},
   "source": [
    "# 15-01 · Открытие и чтение файлов\n",
    "\n",
    "Практика к разделу [«Зачем нужны файлы? Открытие и чтение»](../../site/chapters/glava-15/15-01-zachem-fajly.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f60608b",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Открывать и читать файлы через with."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68e1be55",
   "metadata": {},
   "source": [
    "## Подготовка — создаём учебный файл"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2d328511",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:53.780744Z",
     "iopub.status.busy": "2026-08-14T20:58:53.780631Z",
     "iopub.status.idle": "2026-08-14T20:58:53.797707Z",
     "shell.execute_reply": "2026-08-14T20:58:53.797140Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Учебный файл создан.\n"
     ]
    }
   ],
   "source": [
    "with open(\"privet.txt\", \"w\") as f:\n",
    "    f.write(\"Привет из файла!\\nЭто вторая строка.\")\n",
    "print(\"Учебный файл создан.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b97939c2",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7476c23a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:53.798678Z",
     "iopub.status.busy": "2026-08-14T20:58:53.798559Z",
     "iopub.status.idle": "2026-08-14T20:58:53.801174Z",
     "shell.execute_reply": "2026-08-14T20:58:53.800671Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Привет из файла!\n",
      "Это вторая строка.\n"
     ]
    }
   ],
   "source": [
    "with open(\"privet.txt\", \"r\") as file:\n",
    "    content = file.read()\n",
    "print(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ead95177",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Чтение несуществующего файла вызывает FileNotFoundError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7990930e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:53.802183Z",
     "iopub.status.busy": "2026-08-14T20:58:53.802016Z",
     "iopub.status.idle": "2026-08-14T20:58:53.828082Z",
     "shell.execute_reply": "2026-08-14T20:58:53.827475Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'nesuschestvuyuschij_fajl.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mFileNotFoundError\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m open(\u001b[33m\"nesuschestvuyuschij_fajl.txt\"\u001b[39m, \u001b[33m\"r\"\u001b[39m)\n",
      "\u001b[31mFileNotFoundError\u001b[39m: [Errno 2] No such file or directory: 'nesuschestvuyuschij_fajl.txt'"
     ]
    }
   ],
   "source": [
    "open(\"nesuschestvuyuschij_fajl.txt\", \"r\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7d32882",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Создайте файл со своим именем и городом, затем прочитайте и выведите его содержимое."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "253f5a11",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:53.829332Z",
     "iopub.status.busy": "2026-08-14T20:58:53.829209Z",
     "iopub.status.idle": "2026-08-14T20:58:53.831791Z",
     "shell.execute_reply": "2026-08-14T20:58:53.831388Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cartesian\n",
      "Москва\n"
     ]
    }
   ],
   "source": [
    "with open(\"o_sebe.txt\", \"w\") as f:\n",
    "    f.write(\"Cartesian\\nМосква\")\n",
    "\n",
    "with open(\"o_sebe.txt\", \"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
}
