6 Howler: Working with files and STDOUT

 
envelope

In Harry Potter, a "Howler" is a nasty-gram that arrives by owl at Hogwarts. It will tear itself open, shout a blistering message at the recipient, and then combust. In this exercise, we’re going to write a program that will transform text into a rather mild-mannered version of a Howler by MAKING ALL THE LETTERS UPPERCASE. The text that we’ll process will be given as a single, positional argument.

For instance, if our program is given the input, "How dare you steal that car!", it should scream back "HOW DARE YOU STEAL THAT CAR!" Remember spaces on the command line delimit arguments, so multiple words need to be enclosed in quotes to be considered one argument:

$ ./howler.py 'How dare you steal that car!'
HOW DARE YOU STEAL THAT CAR!
howler

The argument to the program may also name a file, in which case we need to read the file for the input:

$ ./howler.py ../inputs/fox.txt
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

Our program will also accept an -o or --outfile option that names an output file into which the output text should be written. In that case, nothing will be printed on the command line:

$ ./howler.py -o out.txt 'How dare you steal that car!'
treecar

And there should now be a file called out.txt that has the output:

$ cat out.txt
HOW DARE YOU STEAL THAT CAR!

In this exercise, you will learn to:

6.1  Reading files

6.6  Review

sitemap