Lab 8: Part 3. Reading & Writing Files
The lecture notes on files may be helpful during this part of the lab.
To get some practice reading from and writing to files, we've supplied you with the following text files in the starter directory:
nums.txtContains the numbers one through 10, each on its own line.
nums2.txtAn alternate version of
birds.txtContains a list of bird names, one per line.
fruit.txtContains a list of fruits and their categories, one per line, with the fruit and category names separated by a single space.
metals.txtContains a list of a few metallic elements, along with their densities, with a colon and a space in between the element name and density value.
meetings.txtContains a list of meetings, with a meeting name, a start time, and a duration separated by commas. The time of day is two integers (one for the hour and one for the minutes) separated by a colon, while the duration is a floating-point number representing hours.
You will have to create your own code file for this part of the lab,
Work through the exercises below...
Write a function named
showTop which accepts a file name as an
argument, and which reads that file and prints out the first three lines
of text in the file. Test it on
nums.txt, and you should see:
one two three
showBottom should work like
showTop, except it should print the
last three lines from the file instead of the first three. Testing on
nums.txt should result in:
eight nine 10
listLines should accept a file name as an argument, and print the
entire contents of that file, but it should add a line number at the
start of each line, separated from the actual line by a single space. The
first line number should be 1 (not 0). Here's what the result would look
like if applied to
1 one 2 two 3 three 4 4 5 five 6 six 7 seven 8 eight 9 nine 10 10
copyFile should take two different file names as arguments, and it
should open the first file, read in the contents, then open the second
file, and write out a copy of the first file (so that at the end, the two
files end up as copies of each other). Be careful that you don't
accidentally overwrite one of the starter files when you're testing this!
It won't have any return value or printed output... but you should check
that it creates an exact copy of the file you're trying to copy (you
could open both files in a text editor). You could test it with:
writeReport should have two parameters: a list of tuples which each
consist of a string followed by a number, and a string which is a
filename. It should open the target file, erase any old contents (this
happens automatically if you open in write mode) and write out the
strings and numbers from the report, one pair per line. The string should
come first, then a colon, then a space, and then the number. So for
example, if you call:
writeReport( [("titanium", 4.507), ("iron", 7.874), ("copper", 8.96)], "metals.txt" )
Then the file 'metals.txt' should contain the following text:
titanium: 4.507 iron: 7.874 copper: 8.96
readReport does the opposite of
writeReport: it reads data from a
file into a list of tuples. It should accept one argument, which is a
filename, and it must read information from that filename and return a
list of tuples, each consisting of a string followed by a number. The
format in the file will be the same as the format shown in
a string, then a colon followed by a space, and finally the associated
number. If you already got
writeReport working using the test above,
should return the following value:
[("titanium", 4.507), ("iron", 7.874), ("copper", 8.96)]
Hint: you can use the
.split method of
strings with an argument to specify what character or sequence of
characters should be the split point. For this part, using
": " as the
split point is quite efficient.
Optional Challenge Exercises
swapFiles should take two filenames as arguments, and it should swap
the contents of the files. This is not too hard, but it's a bit more
challenging than it might seem at first :)
listEvents should take a single argument which is a string naming a
file to read data from. It should read data from that file specifying
calendar events, and then print out a listing of each event in a modified
events2.txt files each have data which lists
events as three comma-separated fields: an event name, a start time in
hour:minute format, and a duration as a floating-point number of hours.
The start times use hours in a 24-hour format, so 3 p.m. would be listed
as 15. If
listEvents is called like this:
the table it prints should look like this:
CS 111 (9:30-10:30) Major advising (11:00-12:00) Department meeting (12:30-1:30) Research time (15:30-17:00) Home with baby (18:00-6:30)
Note that even after you've figured out how to extract information from the file and convert it to numbers, you will have to do some math to convert the duration-in-hours into an end-time... one strategy is to first convert the number of hours and minutes into a time-in-fractional-hours and then convert that back to hours-and-minutes afterwards.
Table of Contents
- Lab 8 Home
- Part 0: Warm-Up
- Part 1: Loop Tracing & Debugging
- Part 2: Testing with Optimism
- Part 3: Reading & Writing Files
- Knowledge Check