{ "cells": [ { "cell_type": "markdown", "metadata": { "hide_cell": true }, "source": [ "Make me look good. Click on the cell below and press Ctrl+Enter." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide_cell": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "HTML(open('css/custom.css', 'r').read())" ] }, { "cell_type": "markdown", "metadata": { "hide_cell": true }, "source": [ "
SM286D · Introduction to Applied Mathematics with Python · Spring 2020 · Uhan
\n", "\n", "
Lesson 9.
\n", "\n", "

Files and exceptions

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## This lesson..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Reading text files\n", "- Writing text files\n", "- JSON files\n", "- `try`-`except` blocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading text files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- In the same folder as this notebook, there is a file called `demo.txt`.\n", "\n", "- The code below reads from `demo.txt`, and stores its contents as a string in a variable called `contents`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a text file example.\n", "We will use it to demonstrate how to work with text files in class.\n", "This is the last line in the file.\n" ] } ], "source": [ "# Read demo.txt and print its contents\n", "with open(\"demo.txt\") as file_object:\n", " contents = file_object.read()\n", " print(contents)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Line 2 opens `demo.txt` and creates a file object called `file_object`.\n", " - The `with` keyword closes the file once we no longer need access to it.\n", " \n", "- Line 3 uses the `.read()` method of a file object to read the contents of the file as a string.\n", "\n", "- You can also read a text file one line at a time. \n", " - You will be asked to do this in the classwork problems below. See page 187 of PCC.\n", "\n", "- _Note._ When Python reads from a text file, it interprets all text in the file as a string. \n", " - If you read in a number, and want to work with that value as a number, you'll need to convert it using `int()` or `float()`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### File paths" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- What if the file you wanted to read was not in the same folder as this notebook?\n", "\n", "- In this case, you would need to provide the __path__ to the file.\n", "\n", "- You can either provide:\n", " - a **relative file path**, which gives the location of the file relative to the directory where the currently running program file is stored, or \n", " - an **absolute file path**, which tells Python exactly where the file is on your computer regardless of where the program that's being executed is stored. \n", " \n", "- Note that the Windows operating system uses a backslash (\\\\) when displaying file paths, while Unix-based systems use a forward slash (/). Python will allow you to use forward slashes in your code even on Windows. If you want to use backslashes, you will need to use `\\\\` instead of `\\` in order to tell Python to interpret them correctly. For example, you would want to write a path like the following in Windows if you want to use backslashes: \n", "\n", "```\n", "C:\\\\path\\\\to\\\\file.txt\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing text files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To write text to a file, you need to call `open()` with a second argument telling Python that you want to write to the file. \n", "\n", "- The code below asks the user to enter some text to be stored in a file, and then writes it to the file `write_example.txt`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter some text to be stored in a file: I am trapped inside a computer.\n" ] } ], "source": [ "# Ask user for input\n", "file_contents = input(\"Enter some text to be stored in a file: \")\n", "\n", "# Write input to file\n", "with open(\"write_example.txt\", 'w') as file_object:\n", " file_object.write(file_contents)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Note that the second argument for `open()` can one of several things, including:\n", "\n", "| Character | Meaning |\n", "|:---------:|:--------|\n", "| `'r'` | read-only mode |\n", "| `'w'` | write mode: first erase file if it exists |\n", "| `'a'` | append mode: add to end of file if it exists |\n", "\n", "- There are a few others: [here's the documentation](https://docs.python.org/3.7/library/functions.html#open).\n", " \n", "- If you omit the second argument, Python opens the file in read-only mode by default.\n", "\n", "- The `open()` function will automatically create the file you're writing to if it doesn't already exist. \n", "\n", "- Be careful when using the write mode argument `'w'`!\n", " - If the file does exist and you use the `'w'` mode to open the file, `open()` will **ERASE** the contents of the file before returning the file object. 😱\n", " - Unless you know you want to replace the contents of an existing file, you should open the file with the `'a'` or append mode argument." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The JavaScript Object Notation (JSON) format was originally developed for JavaScript.\n", "\n", "- It has become a common format used by many languages, including Python.\n", "\n", "- The `json` module allows you to \n", " - dump simple Python data structures into a JSON file, and\n", " - load the data from a JSON file. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The code below uses `json.dump()` to write a list to a JSON file:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "# Here's a list that I want to write to a file\n", "shopping_list = ['Carrots', 'Potatoes', 'Garlic', 'Chuck Roast', 'Beef Broth']\n", "\n", "# Write the list to a file\n", "with open('MyShoppingList.json', mode='w') as my_fileobj:\n", " json.dump(shopping_list, my_fileobj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The code below reads the file `MyShoppingList.json` and stores the contents in the variable `sl`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Read MyShoppingList.json and load into sl\n", "with open('MyShoppingList.json', mode='r') as fileobj:\n", " sl = json.load(fileobj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- `sl` should be the same as `shopping_list`. Did it work?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Carrots', 'Potatoes', 'Garlic', 'Chuck Roast', 'Beef Broth']\n", "\n" ] } ], "source": [ "# Print sl\n", "print(sl)\n", "\n", "# What is the type of sl?\n", "print(type(sl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Note that the contents of the original list, `shopping_list`, are preserved in `sl` as well as the structure of the data. \n", " - You can see from the output above that `sl` is a list just like the original data was in `shopping_list`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## try-except blocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- An __exception__ is a special object that Python creates when an error occurs.\n", "\n", "- When an error occurs, \"an exception is raised\".\n", "\n", "- A `try`-`except` block asks Python to do something, but it also tells Python what to do if an exception is raised." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Example.__\n", "A common error when working with files is that the provided file name is incorrect. The code below illustrates this error by attempting to load the file `YourShoppingList.json` instead of the correct file `MyShoppingList.json`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'YourShoppingList.json'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Read file\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfileobj\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0msl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfileobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'YourShoppingList.json'" ] } ], "source": [ "filename = 'YourShoppingList.json'\n", "\n", "# Read file\n", "with open(filename, mode='r') as fileobj:\n", " sl = json.load(fileobj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use a `try`-`except` block to handle this type of error and provide the user with a \"friendly\" error message if they enter a file name that doesn't exist." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm sorry, the file YourShoppingList.json does not exist!\n" ] } ], "source": [ "filename = 'YourShoppingList.json'\n", "\n", "# Try opening the file\n", "# If the file doesn't exist, catch the exception\n", "# and print a \"friendly\" error message.\n", "try:\n", " with open(filename, mode='r') as fileobj:\n", " sl = json.load(fileobj)\n", "except FileNotFoundError:\n", " print(f\"I'm sorry, the file {filename} does not exist!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Example.__\n", "`try`-`except` blocks also give you the option to write multiple except blocks to handle different types of errors in different ways. \n", "\n", "The code below asks the user to input two integers and attempts to divide them. If the user enters a value of 0 for the second number, the `ZeroDivisionError` results, and the user is provided with an appropriate warning about division by zero. \n", "\n", "If the user enters text-based values (like one or two) instead of numeric values or enters floating point numbers instead of integers, then the user gets a different error message. \n", "\n", "Run the code below twice, and make a different error each time to see how Python handles it. Note that the final `except` statement contains a \"generic\" error handling message that would be executed if any exception occurs other than a `ZeroDivisionError` or a `ValueError`." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter two integers and I'll attempt to divide them.\n", "First number: 99\n", "Second number: 0\n", "You can't divide by zero!\n" ] } ], "source": [ "# Ask user for input\n", "print(\"Enter two integers and I'll attempt to divide them.\")\n", "first_number = input(\"First number: \")\n", "second_number = input(\"Second number: \")\n", "\n", "# Try dividing the two numbers\n", "# Catch exceptions and print appropriate error messages\n", "try:\n", " answer = int(first_number) / int(second_number)\n", " print(answer)\n", "except ZeroDivisionError:\n", " print(\"You can't divide by zero!\")\n", "except ValueError:\n", " print(\"Please enter integer, numeric values for the First and Second numbers.\")\n", "except:\n", " print(\"Something else went wrong.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classwork" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 1 (PCC 10-1: Learning Python).__\n", "Read pages 184-190 of PCC Chapter 10.\n", "\n", "Open a blank file in your text editor and write a few lines summarizing what you’ve learned about Python so far. Start each line with the phrase \"In Python you can...\" Save the file as `learning_python.txt` in the same directory as your exercises from this chapter. Write a program that reads the file and prints what you wrote three times:\n", "1. Print the contents once by reading in the entire file.\n", "2. Print the contents once by looping over each line.\n", "3. Print the contents once by storing the lines in a list and then working with them outside the `with` block." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In Python you can write fun loops with for statements. \n", "In Python you can branch using if-elif-else statements.\n", "In Python you can do lots of cool math with the numpy package. \n", "\n", "\n", "In Python you can write fun loops with for statements.\n", "In Python you can branch using if-elif-else statements.\n", "In Python you can do lots of cool math with the numpy package.\n", "\n", "\n", "In Python you can write fun loops with for statements.\n", "In Python you can branch using if-elif-else statements.\n", "In Python you can do lots of cool math with the numpy package.\n" ] } ], "source": [ "# Read learning_python.txt, and print contents\n", "# by reading in the entire file\n", "with open(\"learning_python.txt\") as file_object:\n", " contents = file_object.read()\n", " print(contents)\n", " print(\"\\n\")\n", "\n", "# Read learning_python.txt, and print contents\n", "# by looping over each line.\n", "# See page 187 pf PCC Chapter 10.\n", "with open(\"learning_python.txt\") as file_object:\n", " for line in file_object: \n", " print(line.rstrip())\n", " print(\"\\n\")\n", "\n", "# Read learning_python.txt, and print contents\n", "# by storing the lines in a list and then working\n", "# with them outside the with block.\n", "# See page 188 pf PCC Chapter 10.\n", "with open(\"learning_python.txt\") as file_object:\n", " lines = file_object.readlines()\n", " \n", "for line in lines: \n", " print(line.rstrip())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 2 (PCC 10-2: Learning C).__ You can use the `replace()` method to replace any word in a string with a different word. Here’s a quick example showing how to replace \"dog\" with \"cat\" in a sentence:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I really like cats.\n" ] } ], "source": [ "message = \"I really like dogs.\"\n", "\n", "# Replace \"dog\" with \"cat\" in message\n", "# Store new string in message\n", "message = message.replace(\"dog\", \"cat\")\n", "\n", "print(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read in each line from the file you just created, `learning_python.txt`, and replace the word Python with the name of another language, such as C. Print each modified line to the screen." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In C you can write fun loops with for statements. \n", "In C you can branch using if-elif-else statements.\n", "In C you can do lots of cool math with the numpy package. \n" ] } ], "source": [ "# Read learning_python.txt, print its contents\n", "# and replace \"Python\" with \"C\"\n", "with open(\"learning_python.txt\") as file_object:\n", " contents = file_object.read()\n", " print(contents.replace(\"Python\",\"C\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 3 (PCC 10-3: Guest).__ Write a program that prompts the user for their name. When they respond, write their name to a file called `guest.txt`." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name? Nelson\n" ] } ], "source": [ "# Ask user for input\n", "name = input(\"What is your name? \")\n", "\n", "# Write user input to file\n", "with open(\"guest.txt\", 'w') as file_object:\n", " file_object.write(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 4. (PCC 10-4: Guest Book)__.\n", "Write a `while` loop that prompts users for their name. When they enter their name, print a greeting to the screen and add a line recording their visit in a file called `guest_book.txt`. Make sure each entry appears on a new line in the file." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name? (enter q to quit) Jay\n", "What is your name? (enter q to quit) Will\n", "What is your name? (enter q to quit) Nelson\n", "What is your name? (enter q to quit) q\n" ] } ], "source": [ "while True: \n", " # Ask user for input\n", " name = input(\"What is your name? (enter q to quit) \")\n", " \n", " # If user inputs \"q\", then stop asking for input\n", " # Otherwise, write user input to file\n", " if name == 'q':\n", " break\n", " else:\n", " with open(\"guest_book.txt\", 'a') as file_object:\n", " file_object.write(f\"{name}\\n\")\n", " \n", "# Note: appending to a file that doesn't exist just works like 'w':\n", "# a new file is created. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 5 (PCC 10-6: Addition)__. One common problem when prompting for numerical input occurs when people provide text instead of numbers. When you try to convert the input to an `int`, you’ll get a `ValueError`. Write a program that prompts for two numbers. Add them together and print the result. Catch the `ValueError` if either input value is not a number, and print a friendly error message. Test your program by entering two numbers and then by entering some text instead of a number." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number: 42\n", "Enter a second number: this is not a number\n", "You need to enter integers!\n" ] } ], "source": [ "# Ask user for input\n", "num1 = input(\"Enter a number: \")\n", "num2 = input(\"Enter a second number: \")\n", "\n", "# Convert user input to integers and add\n", "# If error occurs, print message.\n", "try:\n", " print(f\"The sum of the two numbers is {int(num1) + int(num2)}.\")\n", "except ValueError:\n", " print(\"You need to enter integers!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 6 (PCC 10-7: Addition Calculator).__ Wrap your code from Problem 5 in a `while` loop so the user can continue entering numbers even if they make a mistake and enter text instead of a number." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number (enter d when done): 1\n", "Enter a number (enter d when done): 2\n", "Enter a number (enter d when done): three\n", "Numbers must be integers. Please continue.\n", "Enter a number (enter d when done): 4\n", "Enter a number (enter d when done): d\n", "The sum of the numbers is 7.\n" ] } ], "source": [ "# List of numbers to add\n", "nums = []\n", "\n", "while True:\n", " # Ask for user input\n", " num = input(\"Enter a number (enter d when done): \")\n", " \n", " # If user inputs \"d\", then stop asking for input\n", " # Otherwise, convert input to integer and add to \n", " # list of numbers to add\n", " # If conversion is not successful, print error message\n", " if num == 'd':\n", " break\n", " else:\n", " try:\n", " nums.append(int(num))\n", " except ValueError:\n", " print(\"Numbers must be integers. Please continue.\")\n", "print(f\"The sum of the numbers is {sum(nums)}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 7 (PCC 10-8: Cats and Dogs).__\n", "Make two files, `cats.txt` and `dogs.txt`. Store at least three names of cats in the first file and three names of dogs in the second file. Write a program that tries to read these files and print the contents of the file to the screen. Wrap your code in a `try-except` block to catch the `FileNotFoundError`, and print a friendly message if a file is missing. Move one of the files to a different location on your system, and make sure the code in the except block executes properly." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Thomas\n", "Sammy\n", "Firepaw\n", "\n", "Lassie\n", "Red\n", "Spot\n", "Could not locate dogs1.txt.\n" ] } ], "source": [ "def printcontents(file):\n", " \"\"\"Print contents of file to screen\"\"\"\n", " # Read file and print its contents\n", " # If file doesn't exist, print error message\n", " try: \n", " with open(file) as file_object:\n", " contents = file_object.read()\n", " print(contents)\n", " except FileNotFoundError:\n", " print(f\"Could not locate {file}.\")\n", " \n", "printcontents(\"cats.txt\")\n", "printcontents(\"dogs.txt\")\n", "printcontents(\"dogs1.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 8 (PCC 10-9: Silent Cats and Dogs).__ \n", "Read pages 200-201 of PCC Chapter 10..\n", "\n", "Modify your `except` block in Problem 7 to fail silently if either file is missing." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Thomas\n", "Sammy\n", "Firepaw\n", "\n", "Lassie\n", "Red\n", "Spot\n" ] } ], "source": [ "def printcontents(file):\n", " \"\"\"Print contents of file to screen\"\"\"\n", " # Read file and print its contents\n", " # If file doesn't exist, just let it go\n", " try: \n", " with open(file) as file_object:\n", " contents = file_object.read()\n", " print(contents)\n", " except FileNotFoundError:\n", " pass\n", " \n", "printcontents(\"cats.txt\")\n", "printcontents(\"dogs.txt\")\n", "printcontents(\"dogs1.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 9. (PCC 10-10: Common Words).__\n", "Visit _[Project Gutenberg](http://gutenberg.org/ )_ and find a few texts you’d like to analyze. \n", "\n", "Download the text files for these works, as follows. We're going to use the `wget` module. This does not come with the standard distribution of Python so we'll install it. \n", "\n", "- Click on the Windows Start button, find __Anaconda3 (64-bit)__, and then select __Anaconda Prompt__. This will bring up a terminal window.\n", "\n", "- In the window, type \n", "\n", "```\n", "pip install wget\n", "```\n", "- Now close the window.\n", "\n", "Once you have `wget` installed, you can run the following code, which downloads the text of _Heart of Darkness_ by Joseph Conrad into a text file called `HOD.txt`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HOD.txt'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import wget\n", "\n", "# Download the file at target_url\n", "target_url = 'http://www.gutenberg.org/files/526/526.txt'\n", "wget.download(target_url, 'HOD.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use the `count()` method to find out how many times a word or phrase appears in a string. For example, the following code counts the number of times \"row\" appears in a string:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = \"Row, row, row your boat\"\n", "line.count('row')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is slightly different. Why?\n", "line.lower().count('row')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that converting the string to lowercase using `lower()` catches all appearances of the word you’re looking for, regardless of how it’s formatted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a program that reads the files you found at Project Gutenberg (pick at least one other book besides _Heart of Darkness_) and determines how many times the word \"the\" appears in each text.\n", "\n", "_Notes._\n", "\n", "- `wget.download` adds `(1)`, `(2)`, etc. to the file name if the file you are downloading already exists in the current directory.\n", "- If you get a `UnicodeDecodeError`, you may need to pass `encoding='utf-8'` to `open()`, like this:\n", "\n", " ```python\n", " with open(filename, encoding='utf-8') as file_object:\n", " ...\n", " ```" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The word 'the' appears 3402 times in Heart of Darkness.\n", "The word 'the' appears 7990 times in Pride and Prejudice.\n" ] } ], "source": [ "# Download the file at target_url\n", "target_url = 'http://www.gutenberg.org/files/1342/1342-0.txt'\n", "wget.download(target_url, 'PrideAndPrejudice.txt')\n", "\n", "# Create dictionary mapping book titles to file names\n", "book_list = {'Heart of Darkness' : 'HOD.txt', \n", " 'Pride and Prejudice' : 'PrideAndPrejudice.txt'}\n", "\n", "# Iterate over the title-filename pairs\n", "# Count the number of times \"the\" appears\n", "for title, filename in book_list.items():\n", " with open(filename, encoding='utf-8') as file_object:\n", " contents = file_object.read()\n", "\n", " print(f\"The word 'the' appears {contents.lower().count('the')} times in {title}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 10 (PCC 10-11: Favorite Number).__\n", "Write a function that prompts for the user’s favorite number. Use `json.dump()` to store this number in a file. \n", "Write a separate function that reads this value from the file and prints the message, `I know your favorite number! It’s ____.`" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your favorite number: 42\n", "I know your favorite number! It is 42.\n" ] } ], "source": [ "import json\n", "\n", "def get_favorite_number(filename):\n", " \"\"\"Ask user for favorite number and write it to a file\"\"\"\n", " # Ask for user input\n", " num = input(\"Enter your favorite number: \")\n", " \n", " # Write JSON file with user input\n", " with open(filename, 'w') as file_object:\n", " json.dump(num, file_object)\n", "\n", "def recover_favorite_number(filename):\n", " \"\"\"Read favorite number from a file and print it\"\"\"\n", " # Read JSON file containing favorite number\n", " with open(filename, 'r') as file_object:\n", " number = json.load(file_object)\n", " print(f\"I know your favorite number! It is {number}.\")\n", "\n", "# Write and read user's favorite number\n", "# using functions defined above\n", "filename = 'favorite_number.json' \n", "get_favorite_number(filename)\n", "recover_favorite_number(filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Problem 11 (PCC 10-12: Favorite Number Remembered).__ Combine the two functions from Problem 10 into one function. If the number is already stored, report the favorite number to the user. If not, prompt for the user’s favorite number and store it in a file. Call the function twice to see that it works." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your favorite number: 11\n", "I know your favorite number! It is 11.\n" ] } ], "source": [ "import json\n", "\n", "def favorite_number(filename):\n", " \"\"\"\n", " If the file doesn't exist, ask user for favorite number and write it to the file.\n", " Otherwise, read the file and print the favorite number.\n", " \"\"\"\n", " # Try reading the JSON file and printing the favorite number.\n", " # If the file is not found, ask for user input and \n", " # write a JSON file with the user input.\n", " try: \n", " with open(filename, 'r') as file_object:\n", " number = json.load(file_object)\n", " print(f\"I know your favorite number! It is {number}.\")\n", " except FileNotFoundError:\n", " num = input(\"Enter your favorite number: \")\n", " with open(filename, 'w') as file_object:\n", " json.dump(num, file_object)\n", "\n", "# Write and read user's favorite number using function defined above\n", "filename = 'favorite_number_11.json' \n", "favorite_number(filename)\n", "favorite_number(filename)" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }