{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7c6ff718",
   "metadata": {},
   "source": [
    "# 15-02 · Строка за строкой\n",
    "\n",
    "Практика к разделу [«Строка за строкой»](../../site/chapters/glava-15/15-02-stroka-za-strokoj.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb00d72",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить построчное чтение файлов."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c54e6701",
   "metadata": {},
   "source": [
    "## Подготовка — создаём учебный файл"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cc7fafe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:54.580510Z",
     "iopub.status.busy": "2026-08-14T20:58:54.580410Z",
     "iopub.status.idle": "2026-08-14T20:58:54.601747Z",
     "shell.execute_reply": "2026-08-14T20:58:54.601301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Учебный файл списка покупок создан.\n"
     ]
    }
   ],
   "source": [
    "spisok = [\"яблоки\", \"хлеб\", \"молоко\", \"сыр\"]\n",
    "with open(\"spisok.txt\", \"w\") as f:\n",
    "    f.writelines(item + \"\\n\" for item in spisok)\n",
    "print(\"Учебный файл списка покупок создан.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d0f324d",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8869d4b1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:54.602755Z",
     "iopub.status.busy": "2026-08-14T20:58:54.602644Z",
     "iopub.status.idle": "2026-08-14T20:58:54.605109Z",
     "shell.execute_reply": "2026-08-14T20:58:54.604684Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "яблоки\n",
      "хлеб\n",
      "молоко\n",
      "сыр\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok.txt\", \"r\") as file:\n",
    "    for line in file:\n",
    "        print(line.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66fba737",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "27c6d268",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:54.606103Z",
     "iopub.status.busy": "2026-08-14T20:58:54.605941Z",
     "iopub.status.idle": "2026-08-14T20:58:54.608391Z",
     "shell.execute_reply": "2026-08-14T20:58:54.608012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 строк\n",
      "яблоки\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok.txt\", \"r\") as file:\n",
    "    lines = file.readlines()\n",
    "\n",
    "print(len(lines), \"строк\")\n",
    "print(lines[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a845cb20",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Посчитайте, сколько строк в файле, не используя len(readlines()) — только счётчиком в цикле."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aadeb5c4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:54.609646Z",
     "iopub.status.busy": "2026-08-14T20:58:54.609464Z",
     "iopub.status.idle": "2026-08-14T20:58:54.612046Z",
     "shell.execute_reply": "2026-08-14T20:58:54.611571Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Строк в файле: 4\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "with open(\"spisok.txt\", \"r\") as file:\n",
    "    for line in file:\n",
    "        count += 1\n",
    "\n",
    "print(\"Строк в файле:\", count)"
   ]
  }
 ],
 "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
}
