{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3423f404",
   "metadata": {},
   "source": [
    "# 15-01 · Otwieranie i czytanie plików\n",
    "\n",
    "Praktyka do sekcji „Dlaczego potrzebuję plików? Otwieranie i czytanie”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af07323d",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Otwieranie i czytanie plików przez with."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36b39081",
   "metadata": {},
   "source": [
    "## Przygotowanie – Stwórz plik tutoriala"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2c239d31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:48.230609Z",
     "iopub.status.busy": "2026-08-18T15:52:48.230491Z",
     "iopub.status.idle": "2026-08-18T15:52:48.253402Z",
     "shell.execute_reply": "2026-08-18T15:52:48.252708Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Учебный файл создан.\n"
     ]
    }
   ],
   "source": [
    "with open(\"privet.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"Привет из файла!\\nЭто вторая строка.\")\n",
    "print(\"Учебный файл создан.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8391e010",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a230279",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:48.255135Z",
     "iopub.status.busy": "2026-08-18T15:52:48.254938Z",
     "iopub.status.idle": "2026-08-18T15:52:48.258392Z",
     "shell.execute_reply": "2026-08-18T15:52:48.257842Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Привет из файла!\n",
      "Это вторая строка.\n"
     ]
    }
   ],
   "source": [
    "with open(\"privet.txt\", \"r\", encoding=\"utf-8\") as file:\n",
    "    content = file.read()\n",
    "print(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bf2da58",
   "metadata": {},
   "source": [
    "## Typowy błąd\n",
    "\n",
    "Odczytanie pliku, który nie istnieje, powoduje FileNotFoundError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8d47087b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:48.259594Z",
     "iopub.status.busy": "2026-08-18T15:52:48.259465Z",
     "iopub.status.idle": "2026-08-18T15:52:48.284563Z",
     "shell.execute_reply": "2026-08-18T15:52:48.284266Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'nesuschestvuyuschij_fajl.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mFileNotFoundError\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m open(\u001b[33m\"nesuschestvuyuschij_fajl.txt\"\u001b[39m, \u001b[33m\"r\"\u001b[39m, encoding=\u001b[33m\"utf-8\"\u001b[39m)\n",
      "\u001b[31mFileNotFoundError\u001b[39m: [Errno 2] No such file or directory: 'nesuschestvuyuschij_fajl.txt'"
     ]
    }
   ],
   "source": [
    "open(\"nesuschestvuyuschij_fajl.txt\", \"r\", encoding=\"utf-8\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b943866",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadań ★\n",
    "\n",
    "Stwórz plik z nazwą i miastem, a następnie przeczytaj i wyjdź jego treść."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "34ea17ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:48.285800Z",
     "iopub.status.busy": "2026-08-18T15:52:48.285690Z",
     "iopub.status.idle": "2026-08-18T15:52:48.288410Z",
     "shell.execute_reply": "2026-08-18T15:52:48.287986Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cartesian\n",
      "Москва\n"
     ]
    }
   ],
   "source": [
    "with open(\"o_sebe.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"Cartesian\\nМосква\")\n",
    "\n",
    "with open(\"o_sebe.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    print(f.read())"
   ]
  }
 ],
 "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
}
