Homework Assignment 9

A Multi-File Search Program

CS 1510
Introduction to Computing
Fall Semester 2014

Due: Saturday, November 8, at 10:00 AM


This assignment extends a program you wrote for Lab 8, grep. The focus of the assignment is to practice breaking a problem down into smaller pieces. You also will get to practice writing a function and working with files and strings.

Download this zip file. Its contents are explained in the next section.

Note the unusual due date.

Code for the Assignment

Use your solution for grep as a starting point. If your code is incomplete and incorrect, feel free to use my implementation of grep from Session 16 as your starting point.

You will also want to use two modules in your program:

You can import these functions into your program by including these lines at the top of the file:

    from str_utils  import multi_find
    from homework09 import get_files

The zip file contains grep.py, str_utils.py, and homework09.py, along with several large text files you can use to test your program.

Spec 1: Multi-File Search

Write a program that

  1. asks the user for a search string,
  2. searches for the string in all the text files in the current directory, and
  3. displays the number of matches in each file, along with each line that contains the search string.

For example, with the files in your test directory:

    >>> search for: er

... will produce the following output.dat:

    *** File searched = dirda-excerpts.txt
    *** Total matches = 46
       4 Some people will never learn anything, for this reason,
       5 because they understand everything too soon.
     108 Ralph Waldo Emerson, Essays / Journals
     109 John Henry Newman, The Idea of a University

    *** File searched = hamlet.txt
    *** Total matches = 1970
      12 Dramatis Personae
      15 Marcellus, Officer.
    4458 Go, bid the soldiers shoot.
    4459 Exeunt marching; after the which a peal of ordnance

    *** File searched = macbeth.txt
    *** Total matches = 1124
       8 Dramatis Personae
      11 MACBETH, Thane of Glamis and Cawdor, a general in the King's army
    2842 Took off her life; this, and what needful else
    2844 We will perform in measure, time, and place.

    *** File searched = midsummer-nights-dream.txt
    *** Total matches = 1308
    2398 Now to scape the serpent's tongue,
    2399 We will make amends ere long;

    *** File searched = test.txt
    *** Total matches = 10
      12 Dramatis Personae
      15 Marcellus, Officer.
      22 Rosencrantz, courtier.
      23 Guildenstern, courtier.

Notice that:

Here is some design and programming advice:

Name your program search_files.py.

Demonstration of Correctness

Run the final version of your program using at least three different search strings with the text files in the zip directory. Make sure you choose strings that demonstrate the features of your program.

Make sure to copy your output.dat file before overwriting it with another run!

If You Don't Get Done. Turn in the latest version of your program that runs. Clearly document in your header block which features are implemented and which features are not implemented.

It is better to turn in incomplete code that works than code that doesn't work.


By the due date and time, submit:

Use the on-line submission system.

Make sure that your program meets the course programming standards.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 8, 2014