|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- {
- "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
- }
|