{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "140987e3",
   "metadata": {},
   "source": [
    "# 15-14 · Читаем файлы: read(), readline(), readlines()\n",
    "\n",
    "Практика к разделу [«Читаем файлы»](../../site/chapters/glava-15/15-14-chitaem-fajly.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6ebca03",
   "metadata": {},
   "source": [
    "## Подготовка"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c7027cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:58.574550Z",
     "iopub.status.busy": "2026-08-18T15:52:58.574412Z",
     "iopub.status.idle": "2026-08-18T15:52:58.596894Z",
     "shell.execute_reply": "2026-08-18T15:52:58.596248Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Готово.\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok2.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"a\\nb\\nc\\n\")\n",
    "print(\"Готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03d7247c",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ed561430",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:58.598802Z",
     "iopub.status.busy": "2026-08-18T15:52:58.598659Z",
     "iopub.status.idle": "2026-08-18T15:52:58.601786Z",
     "shell.execute_reply": "2026-08-18T15:52:58.601382Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a b 3 3\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok2.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    first_line = f.readline().strip()\n",
    "    second_line = f.readline().strip()\n",
    "\n",
    "with open(\"spisok2.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    all_lines = f.readlines()\n",
    "\n",
    "counted = 0\n",
    "with open(\"spisok2.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    for line in f:\n",
    "        counted += 1\n",
    "\n",
    "print(first_line, second_line, len(all_lines), counted)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86785189",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Проверьте, что число строк, посчитанное циклом, совпадает с длиной списка из readlines()."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "189f00fe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:58.602916Z",
     "iopub.status.busy": "2026-08-18T15:52:58.602805Z",
     "iopub.status.idle": "2026-08-18T15:52:58.604819Z",
     "shell.execute_reply": "2026-08-18T15:52:58.604428Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "sovpadaet = counted == len(all_lines)\n",
    "print(sovpadaet)"
   ]
  }
 ],
 "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
}
