CSc 110: File Functions

This assignment should be written in the python programming language. You should only use python features that we have discussed up to this point. If you have questions about whether or not a particular feature can/should be used, ask the instructors! You may use any python version 3.0 or newer.

All of your programming should be well-formatted and easy for the graders to read and comprehend. Each program file should have a header comment at the top that has the following format:

#
# Author: Student Name
# Description:
#    A short description of what this program does!
# 

If any part of your scripts are particularly complex, you should put documentation comments above those lines of code.

For all problems

In this assignment, you will write multiple functions in a single python file named file-functions.py. Each function will do simple task related to files. The description of each function can be found below.

count_lines

Write a function named count_lines. This function takes one parameter which is the name of the file to read. This function will read the file with the provided name, count the number of lines in the file, and return the numeric count.

Below are a few examples:


If the file lines.txt looks like:

orange
apple
orange
apple
orange
apple
grape

Then:

>>> count_lines('lines.txt')
7

If the file lines.txt looks like:

Star Wars
Indiana Jones
LOTR

Then:

>>> count_lines('lines.txt')
3

If the file lines.txt looks like:

A B C D

Then:

>>> count_lines('lines.txt')
1

reverse_file

Write a function named reverse_file which will take a single parameter. The parameter will be the name of the file to reverse. This function will open the file with the name stored in the parameter. It will print out all of the lines of the file in reversed order.

Below are a few examples:


If the file lines.txt looks like:

orange
apple
grape
mango
pear

Then:

>>> reverse_file('lines.txt')
pear
mango
grape
apple
orange

If the file lines.txt looks like:

Star Wars
Indiana Jones
LOTR

Then:

>>> reverse_file('lines.txt')
LOTR
Indiana Jones
Star Wars

If the file lines.txt looks like:

A B C D

Then:

>>> reverse_file('lines.txt')
A B C D

Remember, the list data structure has a reverse() function!

search_file

Write a function named search_file. This function will take two parameters. The first will specify the name of the file. The second will be a string. This function will return a number, indicating the number of lines in the file that the second parameter appears at least one time in.

For example, say there is a file named story.txt in the current working directory with the following contents:

My heart leaps up when I behold
A rainbow in the sky:
So was it when my life began,
So is it now I am a man,
So be it when I shall grow old
Or let me die!
The Child is father of the Man:
And I could wish my days to be
Bound each to each by natural piety.

Below are a few example calls to search_file:

>>> search_file('story.txt', 'rainbow')
1

Clearly, “rainbow” only appears on 1 line in this file, so 1 is returned.

>>> search_file('story.txt', 'each')
1

Notice that the word “each” appears in the file twice, but both appearances are on the same line (line 9), so 1 is still returned.

>>> search_file('story.txt', 'the')
2

the actually appears three times in this file (once on line 2, and twice on line 7), but 2 is returned because it appears in 2 lines total.

>>> search_file('story.txt', 'it')
3

Clearly, it appears on 3 unique lines so 3 is returned.

>>> search_file('story.txt', 'o')
8

The letter “o” appears on 8 lines (all but line 6). As this example suggests, this search should be case-sensitive, because it ignores the upper-case “O” at the beginning of line 6.

>>> search_file('story.txt', 'fellowship')
0

The word “fellowship” never appears, so 0 is returned.

Testing for correctness

This assignment requires printing out precise text messages and notifications. As already mentioned, it is important to test your programs carefully to make sure the output is correct.

Submission and grading

This problem is due on 7/14/2017 at 7:00pm.

You should submit a single python file named file-functions.py, with each of the functions in it. You should not include a main function, or calls to any of the functions. Submit these files to the associated D2L dropbox. Following these turn-in and naming instructions closely is very important, because our grading scripts will depend on some of the details. You may lose points if these instructions are not followed precisely!