@extends('template')
@section('title')
Lab 8: Reading & Writing Files
@stop
@section('content')
# Lab 8: Reading & Writing Files
The [lecture notes on files](/lectures/lec_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.txt` Contains the numbers one through 10, each on its own line.
- `nums2.txt` An alternate version of `nums.txt`.
- `birds.txt` Contains a list of bird names, one per line.
- `fruit.txt` Contains a list of fruits and their categories, one per
line, with the fruit and category names separated by a single space.
- `metals.txt` Contains 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.txt` Contains 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,
called `files.py`**.
Work through the exercises below...
## `showTop`
Partner A
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:
```txt
one
two
three
```
## `showBottom`
Partner B
`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:
```txt
eight
nine
10
```
## `listLines`
Partner A
`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 `nums.txt`:
```txt
1 one
2 two
3 three
4 4
5 five
6 six
7 seven
8 eight
9 nine
10 10
```
## `copyFile`
Partner B
`copyFile` should take two different file names as arguments: the first argument
is the name of the original file, and the second argument is the name of the file that will store a copy of the original file.
You 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:
```py
copyFile("birds.txt", "moreBirds.txt")
```
@include('/labs/lab08/_toc')
@stop