{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4380842b",
   "metadata": {},
   "source": [
    "# 15-15 · Пишем в файлы: write() и режимы r/w/a/x\n",
    "\n",
    "Практика к разделу [«write() и режимы r/w/a/x»](../../site/chapters/glava-15/15-15-pishem-i-rezhimy.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "565d68cc",
   "metadata": {},
   "source": [
    "## Рабочий пример — w, a, x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "027f7d9c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:59.413571Z",
     "iopub.status.busy": "2026-08-18T15:52:59.413458Z",
     "iopub.status.idle": "2026-08-18T15:52:59.432654Z",
     "shell.execute_reply": "2026-08-18T15:52:59.432182Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "старт\n",
      "продолжение\n",
      "\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "with open(\"igra.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"старт\\n\")\n",
    "\n",
    "with open(\"igra.txt\", \"a\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"продолжение\\n\")\n",
    "\n",
    "with open(\"igra.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    posle_dozapisi = f.read()\n",
    "\n",
    "x_failed_as_expected = False\n",
    "try:\n",
    "    with open(\"igra.txt\", \"x\", encoding=\"utf-8\") as f:\n",
    "        pass\n",
    "except FileExistsError:\n",
    "    x_failed_as_expected = True\n",
    "\n",
    "print(posle_dozapisi)\n",
    "print(x_failed_as_expected)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cccdd279",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача — ловушка writelines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1816668d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:59.433660Z",
     "iopub.status.busy": "2026-08-18T15:52:59.433540Z",
     "iopub.status.idle": "2026-08-18T15:52:59.436568Z",
     "shell.execute_reply": "2026-08-18T15:52:59.436007Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Anna\n",
      "Bob\n",
      "\n"
     ]
    }
   ],
   "source": [
    "imena = [\"Anna\", \"Bob\"]\n",
    "with open(\"igroki2.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.writelines(imya + \"\\n\" for imya in imena)\n",
    "\n",
    "with open(\"igroki2.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    igroki_content = f.read()\n",
    "print(igroki_content)"
   ]
  }
 ],
 "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
}
