{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Software Entwicklung \n",
    "\n",
    "## Kapitel 9: IO\n",
    "\n",
    "### 9.4 Dateien schreiben\n",
    "\n",
    "Voraussetzung dafür, dass in eine Datei geschrieben werden kann,\n",
    "ist ein passender Bearbeitungsmodus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"093a Textdatei\", \"w\") as f:\n",
    "    f.write(\"Nürnberg ist schön!\")\n",
    "\n",
    "with open(\"093a Textdatei\", \"r\") as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Wird der Schreib-/Lesekopf zurückgesetzt, kann der bestehende Inhalt überschrieben werden."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"093a Textdatei\", \"w\") as f:\n",
    "    f.write(\"Nürnberg ist nice!\")\n",
    "    pos = f.tell()-5\n",
    "    f.seek(pos)\n",
    "    f.write(\"schön!\")\n",
    "\n",
    "with open(\"093a Textdatei\", \"r\") as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Durch den Bearbeitungsmodus <code>'r+'</code> ist es auch\n",
    "möglich, mit einem Datei-Objekt zu lesen und zu schreiben."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"093a Textdatei\", \"r+\") as f:\n",
    "    f.write(\"Nürnberg ist nice!\")\n",
    "    pos = f.tell()-5\n",
    "    f.seek(pos)\n",
    "    f.write(\"schön!\")\n",
    "    f.seek(0)\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Anders als bei <code>print</code> wird bei <code>write</code>\n",
    "kein Zeilenwechsel angehängt; es muss also ggf. selbst\n",
    "hinzugefügt werden."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"093a Textdatei\", \"r+\") as f:\n",
    "    f.write(\"Zeile 1\")\n",
    "    f.write(\"Zeile 2\\n\")\n",
    "    f.write(\"Zeile 3\\n\")\n",
    "    f.seek(0)\n",
    "    print(f.readline())"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
