{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f4e99fca",
   "metadata": {},
   "source": [
    "# 15-02 · Строка за строкой\n",
    "\n",
    "Практика к разделу [«Строка за строкой»](../../site/chapters/glava-15/15-02-stroka-za-strokoj.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07c810cb",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить построчное чтение файлов."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f62f8213",
   "metadata": {},
   "source": [
    "## Подготовка — создаём учебный файл"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5d84d9b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.043948Z",
     "iopub.status.busy": "2026-08-18T15:52:49.043838Z",
     "iopub.status.idle": "2026-08-18T15:52:49.060227Z",
     "shell.execute_reply": "2026-08-18T15:52:49.059732Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Учебный файл списка покупок создан.\n"
     ]
    }
   ],
   "source": [
    "spisok = [\"яблоки\", \"хлеб\", \"молоко\", \"сыр\"]\n",
    "with open(\"spisok.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.writelines(item + \"\\n\" for item in spisok)\n",
    "print(\"Учебный файл списка покупок создан.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6729fcb1",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0c9a9fd4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.061235Z",
     "iopub.status.busy": "2026-08-18T15:52:49.061116Z",
     "iopub.status.idle": "2026-08-18T15:52:49.063599Z",
     "shell.execute_reply": "2026-08-18T15:52:49.063157Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "яблоки\n",
      "хлеб\n",
      "молоко\n",
      "сыр\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    for line in file:\n",
    "        print(line.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca01bbfc",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "91561a88",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.064532Z",
     "iopub.status.busy": "2026-08-18T15:52:49.064411Z",
     "iopub.status.idle": "2026-08-18T15:52:49.066861Z",
     "shell.execute_reply": "2026-08-18T15:52:49.066377Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 строк\n",
      "яблоки\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"spisok.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    lines = file.readlines()\n",
    "\n",
    "print(len(lines), \"строк\")\n",
    "print(lines[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4dbfdb0",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Посчитайте, сколько строк в файле, не используя len(readlines()) — только счётчиком в цикле."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96895f9f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:49.067744Z",
     "iopub.status.busy": "2026-08-18T15:52:49.067640Z",
     "iopub.status.idle": "2026-08-18T15:52:49.070010Z",
     "shell.execute_reply": "2026-08-18T15:52:49.069548Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Строк в файле: 4\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "with open(\"spisok.txt\", \"r\", encoding=\"utf-8\") 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
}
