{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 6 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 6.2: Reading Text from a File" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Execute this cell to initialize the notebook:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from PythonLabs.Tools import path_to_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T17. Find out where the data file named `quote1.txt` is stored on your computer:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T18. Open the file and save the object returned by `open`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T19. If you call this object's `readline` method you should get the first line in the file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T20. Call `readline` again, but this time save the result in a new variable named `s`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T21. Ask Python to show you the value of `s`, which should be the second line in the file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T22. Call `len` to count the number of characters in `s`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T23. Run the cells above with calls to `readline` and `len` two more times to count the number of characters in the last two lines of the file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T24. If you call `readline` again you should get back an empty string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T25. Call the `close` method to close the file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T26. If you try to read a line from a closed file you should get an error message:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T27. Use the code cell below to define a function named `filesize`. The first version should have only these three lines:\n", "
\n",
    "def filesize(fn):\n",
    "    for line in open(fn):\n",
    "        print(line, end = \"\")\n",
    "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T28. Test the `filesize` function on the file named `quote1.txt`. Does it print every line?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T29. Edit your definition of `filesize` so it looks like the one in Figure6.3: \n", "* add the line that initializes `nchars` to 0\n", "* replace the `print` statement with the assignment that adds the length of the line to `nchars`\n", "* add the `return` statement.\n", "\n", "Run the code cell to update the definition in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T30. Repeat the statement that calls the function. This time you should see the number of characters in the file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }