{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f78d6b20",
   "metadata": {},
   "source": [
    "# 15-20 · Поиск файлов: iterdir() и glob()\n",
    "\n",
    "Практика к разделу [«iterdir() и glob()»](../../site/chapters/glava-15/15-20-poisk-fajlov-glob.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a61a2b2d",
   "metadata": {},
   "source": [
    "## Подготовка"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11cf4339",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:03.564924Z",
     "iopub.status.busy": "2026-08-18T15:53:03.564817Z",
     "iopub.status.idle": "2026-08-18T15:53:03.580583Z",
     "shell.execute_reply": "2026-08-18T15:53:03.580083Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Готово.\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "Path(\"otchet_dir\").mkdir(exist_ok=True)\n",
    "Path(\"otchet_dir/a.txt\").write_text(\"1\", encoding=\"utf-8\")\n",
    "Path(\"otchet_dir/b.csv\").write_text(\"2\", encoding=\"utf-8\")\n",
    "Path(\"otchet_dir/c.txt\").write_text(\"3\", encoding=\"utf-8\")\n",
    "print(\"Готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffb92bfc",
   "metadata": {},
   "source": [
    "## Рабочий пример — мини-проект: отчёт по папке"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba1d2cc2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:03.581596Z",
     "iopub.status.busy": "2026-08-18T15:53:03.581484Z",
     "iopub.status.idle": "2026-08-18T15:53:03.585678Z",
     "shell.execute_reply": "2026-08-18T15:53:03.584810Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a.txt', 'b.csv', 'c.txt']\n",
      "['a.txt', 'c.txt']\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "vse_items = sorted(p.name for p in Path(\"otchet_dir\").iterdir())\n",
    "txt_files = sorted(p.name for p in Path(\"otchet_dir\").glob(\"*.txt\"))\n",
    "print(vse_items)\n",
    "print(txt_files)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6d6748c",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача\n",
    "\n",
    "Постройте отчёт вида «имя — файл/папка» для каждого элемента `otchet_dir`, используя `is_dir()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a62dffb2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:03.587095Z",
     "iopub.status.busy": "2026-08-18T15:53:03.586816Z",
     "iopub.status.idle": "2026-08-18T15:53:03.591079Z",
     "shell.execute_reply": "2026-08-18T15:53:03.590593Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a.txt — файл', 'b.csv — файл', 'c.txt — файл']\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "otchet = []\n",
    "for item in sorted(Path(\"otchet_dir\").iterdir()):\n",
    "    vid = \"папка\" if item.is_dir() else \"файл\"\n",
    "    otchet.append(f\"{item.name} — {vid}\")\n",
    "print(otchet)"
   ]
  }
 ],
 "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
}
