This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: Reading and writing files

Exercise

The best way to practise reading and writing files is to apply them!

Here are two ‘real-world’ files for you to play with. Download the files to the same directory as where you are implementing your code.

Write a function convert_to_lowercase() that takes in an input argument filename (a str). The function will generate a new file that lists each word (in lowercase) from the input file, one word per line.

Assume that filename will always end with .txt. Name the output file by adding _lower to the end of the filename (but before its .txt extension). So if filename is alice.txt, your output file should be called alice_lower.txt.

You should read each line in filename, and split each line into words (delimited by spaces). Convert each word to lowercase, and output each word in the output file, one per line. Do not output empty strings. The words can be mixed with punctuations, like "conversation?'" (splitting this perfectly will be a tokenisation task in text processing and is beyond the scope of our course!) See the sample output below for an example.

The function should return the filename of the output file (e.g. alice_lower.txt).

Sample output

>>> outfilename = convert_to_lowercase("alice.txt")
>>> print(outfilename)
'alice_lower.txt'

Content of the output file alice_lower.txt:

[alice's
adventures
in
wonderland
by
lewis
carroll
1865]
chapter
i.
down
the
rabbit-hole
alice
was
beginning
to
get
very
tired
of
sitting
by
her
sister
on
the
bank,
and
of
having
nothing
to
do:
once
...