You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

093 Schreiben.ipynb 3.2KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "pycharm": {
  7. "name": "#%% md\n"
  8. },
  9. "slideshow": {
  10. "slide_type": "slide"
  11. }
  12. },
  13. "source": [
  14. "# Software Entwicklung \n",
  15. "\n",
  16. "## Kapitel 9: IO\n",
  17. "\n",
  18. "### 9.4 Dateien schreiben\n",
  19. "\n",
  20. "Voraussetzung dafür, dass in eine Datei geschrieben werden kann,\n",
  21. "ist ein passender Bearbeitungsmodus."
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": null,
  27. "metadata": {
  28. "pycharm": {
  29. "name": "#%%\n"
  30. }
  31. },
  32. "outputs": [],
  33. "source": [
  34. "with open(\"093a Textdatei\", \"w\") as f:\n",
  35. " f.write(\"Nürnberg ist schön!\")\n",
  36. "\n",
  37. "with open(\"093a Textdatei\", \"r\") as f:\n",
  38. " print(f.read())"
  39. ]
  40. },
  41. {
  42. "cell_type": "markdown",
  43. "metadata": {
  44. "slideshow": {
  45. "slide_type": "slide"
  46. }
  47. },
  48. "source": [
  49. "Wird der Schreib-/Lesekopf zurückgesetzt, kann der bestehende Inhalt überschrieben werden."
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": null,
  55. "metadata": {
  56. "pycharm": {
  57. "name": "#%%\n"
  58. }
  59. },
  60. "outputs": [],
  61. "source": [
  62. "with open(\"093a Textdatei\", \"w\") as f:\n",
  63. " f.write(\"Nürnberg ist nice!\")\n",
  64. " pos = f.tell()-5\n",
  65. " f.seek(pos)\n",
  66. " f.write(\"schön!\")\n",
  67. "\n",
  68. "with open(\"093a Textdatei\", \"r\") as f:\n",
  69. " print(f.read())"
  70. ]
  71. },
  72. {
  73. "cell_type": "markdown",
  74. "metadata": {
  75. "pycharm": {
  76. "name": "#%% md\n"
  77. },
  78. "slideshow": {
  79. "slide_type": "slide"
  80. }
  81. },
  82. "source": [
  83. "Durch den Bearbeitungsmodus <code>'r+'</code> ist es auch\n",
  84. "möglich, mit einem Datei-Objekt zu lesen und zu schreiben."
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": null,
  90. "metadata": {
  91. "pycharm": {
  92. "name": "#%%\n"
  93. }
  94. },
  95. "outputs": [],
  96. "source": [
  97. "with open(\"093a Textdatei\", \"r+\") as f:\n",
  98. " f.write(\"Nürnberg ist nice!\")\n",
  99. " pos = f.tell()-5\n",
  100. " f.seek(pos)\n",
  101. " f.write(\"schön!\")\n",
  102. " f.seek(0)\n",
  103. " print(f.read())"
  104. ]
  105. },
  106. {
  107. "cell_type": "markdown",
  108. "metadata": {
  109. "slideshow": {
  110. "slide_type": "slide"
  111. }
  112. },
  113. "source": [
  114. "Anders als bei <code>print</code> wird bei <code>write</code>\n",
  115. "kein Zeilenwechsel angehängt; es muss also ggf. selbst\n",
  116. "hinzugefügt werden."
  117. ]
  118. },
  119. {
  120. "cell_type": "code",
  121. "execution_count": null,
  122. "metadata": {
  123. "pycharm": {
  124. "name": "#%%\n"
  125. }
  126. },
  127. "outputs": [],
  128. "source": [
  129. "with open(\"093a Textdatei\", \"r+\") as f:\n",
  130. " f.write(\"Zeile 1\")\n",
  131. " f.write(\"Zeile 2\\n\")\n",
  132. " f.write(\"Zeile 3\\n\")\n",
  133. " f.seek(0)\n",
  134. " print(f.readline())"
  135. ]
  136. }
  137. ],
  138. "metadata": {
  139. "celltoolbar": "Slideshow",
  140. "kernelspec": {
  141. "display_name": "Python 3 (ipykernel)",
  142. "language": "python",
  143. "name": "python3"
  144. },
  145. "language_info": {
  146. "codemirror_mode": {
  147. "name": "ipython",
  148. "version": 3
  149. },
  150. "file_extension": ".py",
  151. "mimetype": "text/x-python",
  152. "name": "python",
  153. "nbconvert_exporter": "python",
  154. "pygments_lexer": "ipython3",
  155. "version": "3.9.9"
  156. }
  157. },
  158. "nbformat": 4,
  159. "nbformat_minor": 1
  160. }