|
11 | 11 | "Python has a built-in open function that allows us to open and play with basic file types. First we will need a file though. We're going to use some IPython magic to create a text file!\n",
|
12 | 12 | "\n",
|
13 | 13 | "## IPython Writing a File \n",
|
14 |
| - "#### This function is specific to jupyter notebooks!" |
| 14 | + "#### This function is specific to jupyter notebooks! Alternatively, quickly create a simple .txt file with sublime text editor." |
15 | 15 | ]
|
16 | 16 | },
|
17 | 17 | {
|
|
38 | 38 | "source": [
|
39 | 39 | "## Python Opening a file\n",
|
40 | 40 | "\n",
|
41 |
| - "We can open a file with the open() function. The open function also takes in arguments (also called parameters). Lets see how this is used:" |
| 41 | + "Let's being by opening the file test.txt that is located in the same directory as this notebook. For now we will work with files located in the same directory as the notebook or .py script you are using.\n", |
| 42 | + "\n", |
| 43 | + "It is very easy to get an error on this step:" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 1, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "ename": "FileNotFoundError", |
| 53 | + "evalue": "[Errno 2] No such file or directory: 'whoops.txt'", |
| 54 | + "output_type": "error", |
| 55 | + "traceback": [ |
| 56 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 57 | + "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", |
| 58 | + "\u001b[1;32m<ipython-input-1-dafe28ee473f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmyfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'whoops.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 59 | + "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'whoops.txt'" |
| 60 | + ] |
| 61 | + } |
| 62 | + ], |
| 63 | + "source": [ |
| 64 | + "myfile = open('whoops.txt')" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "To avoid this error,make sure your .txt file is saved in the same location as your notebook, to check your notebook location, use **pwd**:" |
42 | 72 | ]
|
43 | 73 | },
|
44 | 74 | {
|
45 | 75 | "cell_type": "code",
|
46 | 76 | "execution_count": 2,
|
47 | 77 | "metadata": {},
|
| 78 | + "outputs": [ |
| 79 | + { |
| 80 | + "data": { |
| 81 | + "text/plain": [ |
| 82 | + "'C:\\\\Users\\\\Marcial\\\\Pierian-Data-Courses\\\\Complete-Python-3-Bootcamp\\\\00-Python Object and Data Structure Basics'" |
| 83 | + ] |
| 84 | + }, |
| 85 | + "execution_count": 2, |
| 86 | + "metadata": {}, |
| 87 | + "output_type": "execute_result" |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "pwd" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "**Alternatively, to grab files from any location on your computer, simply pass in the entire file path. **\n", |
| 99 | + "\n", |
| 100 | + "For Windows you need to use double \\ so python doesn't treat the second \\ as an escape character, a file path is in the form:\n", |
| 101 | + "\n", |
| 102 | + " myfile = open(\"C:\\\\Users\\\\YourUserName\\\\Home\\\\Folder\\\\myfile.txt\")\n", |
| 103 | + "\n", |
| 104 | + "For MacOS and Linux you use slashes in the opposite direction:\n", |
| 105 | + "\n", |
| 106 | + " myfile = open(\"/Users/YouUserName/Folder/myfile.txt\")" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 2, |
| 112 | + "metadata": { |
| 113 | + "collapsed": true |
| 114 | + }, |
48 | 115 | "outputs": [],
|
49 | 116 | "source": [
|
50 | 117 | "# Open the text.txt we made earlier\n",
|
|
181 | 248 | {
|
182 | 249 | "cell_type": "code",
|
183 | 250 | "execution_count": 8,
|
184 |
| - "metadata": {}, |
| 251 | + "metadata": { |
| 252 | + "collapsed": true |
| 253 | + }, |
185 | 254 | "outputs": [],
|
186 | 255 | "source": [
|
187 | 256 | "my_file.close()"
|
|
199 | 268 | {
|
200 | 269 | "cell_type": "code",
|
201 | 270 | "execution_count": 9,
|
202 |
| - "metadata": {}, |
| 271 | + "metadata": { |
| 272 | + "collapsed": true |
| 273 | + }, |
203 | 274 | "outputs": [],
|
204 | 275 | "source": [
|
205 | 276 | "# Add a second argument to the function, 'w' which stands for write.\n",
|
|
262 | 333 | {
|
263 | 334 | "cell_type": "code",
|
264 | 335 | "execution_count": 12,
|
265 |
| - "metadata": {}, |
| 336 | + "metadata": { |
| 337 | + "collapsed": true |
| 338 | + }, |
266 | 339 | "outputs": [],
|
267 | 340 | "source": [
|
268 | 341 | "my_file.close() # always do this when you're done with a file"
|
|
321 | 394 | {
|
322 | 395 | "cell_type": "code",
|
323 | 396 | "execution_count": 15,
|
324 |
| - "metadata": {}, |
| 397 | + "metadata": { |
| 398 | + "collapsed": true |
| 399 | + }, |
325 | 400 | "outputs": [],
|
326 | 401 | "source": [
|
327 | 402 | "my_file.close()"
|
|
473 | 548 | "name": "python",
|
474 | 549 | "nbconvert_exporter": "python",
|
475 | 550 | "pygments_lexer": "ipython3",
|
476 |
| - "version": "3.6.2" |
| 551 | + "version": "3.6.1" |
477 | 552 | }
|
478 | 553 | },
|
479 | 554 | "nbformat": 4,
|
|
0 commit comments