Session 15

Reading from and Writing to Files


CS 1510
Introduction to Computing


Opening Exercise: Tally Visitors

Whenever we host preview days and open houses for high school students, I keep track of how many students I meet with, in a form similar to this:

    2014/10/10,2,1

Write a program that allows me to enter such strings, one at a time, and then tallies up how many visitors and prospects I have seen in total. For example:

    >>> First event: 10/10,2,1
    >>> Next event: 10/11,4,1
    >>> Next event: 10/13,1,1
    >>> Next event: 

    Visitors  7
    Prospects 3

For a little extra fun, throw in the average number of students I see each day.

~~~~~

Such a program might look something like this. Notes:

But I have years of data. I don't want to type it all in by hand.



Reading a File

... a file is ...

The same program modified to read from a file:

    event_file = open('attendance.txt','r')

    visitors  = 0
    prospects = 0
    for event_str in event_file:
        event_str = event_str.strip()
        date,visitors_str,prospects_str = event_str.split(',')
        visitors  += int(visitors_str)
        prospects += int(prospects_str)

    print('Visitors ', visitors)
    print('Prospects', prospects)

    event_file.close()

There are ways to write this as sentinel loop, but Python's for...in... statement treats a file as a collection of lines. Notice that string we receive for each line includes its new line character. We have to strip it off by hand.

Otherwise the only new Python constructs we need are the open() function, which creates a file object for us, and the close() method for files, which makes sure the connection to the file out on disk is closed properly.

Accessing external data files gives us the ability to process huge amounts of data as a single collection of lines, without requiring any user interaction at all.



Next Exercise: Report Monthly Visitors

We sometimes like to step back from the mass of data from preview days and see trends in the data. For example, we might like to see how many students visited UNI CS in each calendar year:

    2007 total visitors  24
    2008 total visitors  21
    ...
    2013 total visitors  54
    2014 total visitors  45

Modify this program so that it writes a report of yearly visitors to a file called report_by_year.txt.

A small part of this change is new Python syntax for writing to a file. A bigger part is figuring out how to augment the program's algorithm to make it happen.

~~~~~

Such a program might look something like this. Notes:

Drawing conclusions from this data is difficult without more information. Were there more events some years? Different kinds of events? How many events had zero visitors? There is room to do more analysis -- with a Python program, of course!

Even so, you have just done the department a great service. We need programs like this.



Wrap Up



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 14, 2014