|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Ch. 10: Files and Exceptions" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "attachments": {}, |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "Windows systems use a backslash (`\\`) instead of a forward slash (`/`) when displaying \n", |
| 17 | + "file paths, but you can still use forward slashes in your code." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 4, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [ |
| 25 | + { |
| 26 | + "name": "stdout", |
| 27 | + "output_type": "stream", |
| 28 | + "text": [ |
| 29 | + "3.1415926535\n", |
| 30 | + "8979323846\n", |
| 31 | + "2643383279\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
| 35 | + "source": [ |
| 36 | + "with open('python_work/pi_digits.txt') as file_object:\n", |
| 37 | + " contents = file_object.read()\n", |
| 38 | + "# Python’s rstrip() method removes, or strips, any whitespace characters \n", |
| 39 | + "# from the right side of a string.\n", |
| 40 | + "print(contents.rstrip())" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 10, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [ |
| 48 | + { |
| 49 | + "name": "stdout", |
| 50 | + "output_type": "stream", |
| 51 | + "text": [ |
| 52 | + "/home/voodoo/Projects/Repos/Python-Crash-Course/Ch_10/python_work/pi_digits.txt\n", |
| 53 | + "3.1415926535\n", |
| 54 | + "8979323846\n", |
| 55 | + "2643383279\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "file_path = '/home/voodoo/Projects/Repos/Python-Crash-Course/Ch_10/python_work/'\n", |
| 61 | + "file_name = 'pi_digits.txt'\n", |
| 62 | + "\n", |
| 63 | + "file = file_path + file_name\n", |
| 64 | + "\n", |
| 65 | + "print(file)\n", |
| 66 | + "\n", |
| 67 | + "with open(file, 'r') as file_object:\n", |
| 68 | + " contents = file_object.read()\n", |
| 69 | + "print(contents.rstrip())" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "attachments": {}, |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "If you try to use backslashes in a file path, you’ll get an error because the backslash is \n", |
| 78 | + "used to escape characters in strings. For example, in the path \"C:\\path\\to\\file.txt\", \n", |
| 79 | + "the sequence \\t is interpreted as a tab. If you need to use backslashes, you can escape \n", |
| 80 | + "each one in the path, like this: \"C:\\\\path\\\\to\\\\file.txt\"." |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "attachments": {}, |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "#### Reading line by line" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 12, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "name": "stdout", |
| 98 | + "output_type": "stream", |
| 99 | + "text": [ |
| 100 | + "3.1415926535\n", |
| 101 | + "8979323846\n", |
| 102 | + "2643383279\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "with open(file) as file_object:\n", |
| 108 | + " for line in file_object:\n", |
| 109 | + " print(line.rstrip())" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "attachments": {}, |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "#### Making a List of Lines from a File" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "attachments": {}, |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": {}, |
| 124 | + "source": [ |
| 125 | + "If you want to retain access to a file’s contents outside the with block, you can store the file’s lines in a list inside the block and then work with that list. You can process parts of the file immediately and postpone some processing for later in the program.\n", |
| 126 | + "\n", |
| 127 | + "The readlines() method takes each line from the file and stores it in a list. This list is then assigned to lines, which we can continue to work with after the with block ends. At v we use a simple for loop to print each line from lines. Because each item in lines corresponds to each line in the file, the output matches the contents of the file exactly." |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": 13, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "name": "stdout", |
| 137 | + "output_type": "stream", |
| 138 | + "text": [ |
| 139 | + "3.1415926535\n", |
| 140 | + "8979323846\n", |
| 141 | + "2643383279\n" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "with open(file) as file_object:\n", |
| 147 | + " lines = file_object.readlines()\n", |
| 148 | + "\n", |
| 149 | + "for line in lines:\n", |
| 150 | + " print(line.rstrip())" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "attachments": {}, |
| 155 | + "cell_type": "markdown", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "## Left off on the bottom of pg.188" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": [] |
| 167 | + } |
| 168 | + ], |
| 169 | + "metadata": { |
| 170 | + "kernelspec": { |
| 171 | + "display_name": "Python 3.8.10 64-bit", |
| 172 | + "language": "python", |
| 173 | + "name": "python3" |
| 174 | + }, |
| 175 | + "language_info": { |
| 176 | + "codemirror_mode": { |
| 177 | + "name": "ipython", |
| 178 | + "version": 3 |
| 179 | + }, |
| 180 | + "file_extension": ".py", |
| 181 | + "mimetype": "text/x-python", |
| 182 | + "name": "python", |
| 183 | + "nbconvert_exporter": "python", |
| 184 | + "pygments_lexer": "ipython3", |
| 185 | + "version": "3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]" |
| 186 | + }, |
| 187 | + "orig_nbformat": 4, |
| 188 | + "vscode": { |
| 189 | + "interpreter": { |
| 190 | + "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" |
| 191 | + } |
| 192 | + } |
| 193 | + }, |
| 194 | + "nbformat": 4, |
| 195 | + "nbformat_minor": 2 |
| 196 | +} |
0 commit comments