To start this assignment, download this zip file.
Homework 4d — Files
For each of these problems, you will be using the file processing pattern. See the guide on reading and writing files for help. You will be starting with a blank file for each of these problems. The accompanying lab was designed to get you ready for this. Good luck! :-)
1. Add bullets
Write a program named add_bullets.py
that takes the following arguments, in
this order:
- an input file
- an output file
- a string to use for bullets
The program takes the input file and adds the supplied string, plus a space, before each line.
We have given you an input file called bullets.input.txt
that contains:
here are some lines
add some bullets to them
and have a great day!
When you run your add_bullets.py
program like this:
python add_bullets.py bullets.input.txt bullets.output.txt "@"
Then the file called bullets.output.txt
should contain:
@ here are some lines
@ add some bullets to them
@ and have a great day!
2. Filter NaN
Write a program named filter_nan.py
that takes the following arguments, in
this order:
- an input file
- an output file
The input file will have a set of lines, each one containing either an integer, a float, or “NaN”, which means “not a number”. You need to remove any lines that contain “NaN” and write the output file with all the other lines.
We have given you an input file called nan_input.txt
that contains:
NaN
42
NaN
NaN
3.0
1.23456789
NaN
When you run your filter_nan.py
program like this:
python filter_nan.py nan_input.txt nan_output.txt
then the file nan_output.txt
should contain:
42
3.0
1.23456789
3. Ooooo
Write a program named ooooo.py
that takes the following arguments, in this
order:
- an input file
- an output file
The input file will have a set of lines. You need to replace all vowels with the
letter o
, then write the result to the output file.
We have given you an input file called peter_piper.txt
that contains:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where’s the peck of pickled peppers Peter Piper picked?
When you run your ooooo.py
program like this:
python ooooo.py peter_piper.txt peter_ooooo.txt
then the file peter_ooooo.txt
should contain:
Potor Popor pockod o pock of pocklod poppors,
o pock of pocklod poppors Potor Popor pockod;
of Potor Popor pockod o pock of pocklod poppors,
Whoro’s tho pock of pocklod poppors Potor Popor pockod?
4. Redaction
Write a program named redaction.py
that takes the following arguments, in this
order:
- an input file
- an output file
- a string pattern
The input file will have a set of lines. You need to replace all substrings
equal to the pattern with ****
, then write the result to the output file.
We have given you an input file called peter_piper.txt
that contains:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where’s the peck of pickled peppers Peter Piper picked?
When you run your redaction.py
program like this:
% python redaction.py peter_piper.txt peter_redacted.txt "Peter Piper"
then the file peter_redacted.txt
should contain:
**** picked a peck of pickled peppers,
A peck of pickled peppers **** picked;
If **** picked a peck of pickled peppers,
Where’s the peck of pickled peppers **** picked?
This is only fitting, since Peter Piper is a suspect but has not yet been convicted.
Tests
Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.
Grading
Activity | Points |
---|---|
Add bullets | 5 |
Filter NaN | 5 |
Ooooo | 5 |
Redaction | 5 |
Manual grading will focus on decomposition, fluency, and use of file reading and writing.