Matthew L. Wright
Assistant Professor, St. Olaf College

Homework 13: Python and Files

CS 121 ⋅ Spring 2016

Each of the following exercises asks you to write a program that manipulates files. Post the Python file(s) containing your solutions to Moodle for HW13.

  1. Do Files Exercise #1 from the online Python text. Here is the file studentdata.txt.
  2. Do Files Exercise #2 from the online Python text. Here is the file studentdata.txt.
  3. The file movie_data.txt contains information about popular movies from 2015. Specifically, each line of the file gives the release date, title, worldwide gross revenue (in US dollars), and director for a particular movie. These fields are separated by tab characters. Here is a sample line, with tab characters printed \t:

    12 June 2015\tJurassic World\t1670400637\tColin Trevorrow

    This line indicates that Jurassic World was released on June 12, directed by Colin Trevorrow, and grossed $1,670,400,637.

    Write a program that reads such a file and writes a new file called billions.txt containing only the titles of the movies that grossed more than one billion dollars (one movie per line).

  4. Write a function readWords() that accepts the name of a file and returns a list of all the unique words in the file. While reading the words in the file, your function should convert each word to lower case (hint: review Python string methods).

    Your function header should be:

    def readWords(filename):

    For example, suppose the file pat_sat.txt contains the following text:

    PAT SAT Pat sat on hat
    PAT CAT Pat sat on cat

    A call to readWords("pat_sat.txt") should return the following list:

    ['pat', 'sat', 'on', 'hat', 'cat']

    Make sure your function properly opens and closes the file! You may assume that the file contains only alpabetic characters and white space.