Due: 10:00pm EST on Monday, November 2, 2026.
Instructions: complete this homework using Thonny, because hw7 requires working with external files (and that can be cumbersome in Colab).
Click here to download the notetaker folder to get started. This folder contains text files that you can use as input files to analyze. Inside the notetaker folder, you'll find a file called notetaker.py. Rename this file to be username_notetaker.py. For example, this is how Beyoncé Knowles would name her file: bk101_hw7_notetaker.py. Add all your code for this assignment in this file. For this assignment, you will create and submit a separate testing file (more details below).
Work with text files, reading from and writing to them
Write an interactive program with multiple options for the user
Write tests for functions to demonstate that they work properly
You will write an interactive program that simulates a notetaking app.
This task will help you think about how to work with a text file, making changes to it over time. You will write a notetaker program that will allow the user to create a new notes file and make changes to it, such as appending new data, inserting new data, deleting existing text, overwriting existing text, and replacing existing text. There are sample runs shown below to give you a sense of how your program must work.
Your task is to write these 9 functions:
getInfoFromUser
create
showFile
append
insert
delete
overwrite
replace
notes
You will write the first 8 functions above from scratch, and you are given a skeleton for the notes function. Before diving into the details of the functions, let's take a look at some sample runs of what the program can do.
▶️ happiness.txt example (new file, mostly insert and append)
>>> notes()
Enter the filename: happiness.txt
This file does not exist, creating it for you now...
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: o
Please enter the line number to be overwritten: 25
Sorry, the file happiness.txt does not contain line number 25.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: k
Sorry, that was not a valid command.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: o
Please enter the line number to be overwritten: 1
Sorry, the file happiness.txt does not contain line number 1.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: i
Please enter your note: things that can make people happy
"things that can make people happy" is now the first line of happiness.txt.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 things that can make people happy
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: warm cookies
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: hugs
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: reality tv shows that are really bad
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: sitting near a crackling fire on a cold night
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 things that can make people happy
2 warm cookies
3 hugs
4 reality tv shows that are really bad
5 sitting near a crackling fire on a cold night
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: rainbows
Please enter the replacement word: pot of gold
No occurrences of rainbows found.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: q
See you later!
▶️ colors.txt example (new file, insert, overwrite, replace)
>>> notes()
Enter the filename: colors
This file does not exist, creating it for you now...
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: i
Please enter your note: red
"red" is now the first line of colors.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: i
Please enter your note: blue
"blue" is now the first line of colors.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: i
Please enter your note: green
"green" is now the first line of colors.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: orange
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: a
Please enter your note: yellow
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 green
2 blue
3 red
4 orange
5 yellow
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: XX
Sorry, that was not a valid command.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: o
Please enter the line number to be overwritten: 3
Please enter your note: hot pink
Overwriting line 3 with "hot pink".
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 green
2 blue
3 hot pink
4 orange
5 yellow
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: hot dog
Please enter the replacement word: hamburger
No occurrences of hot dog found.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: ran
Please enter the replacement word: skip
No occurrences of ran found.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: green
Please enter the replacement word: violet
Replaced 1 occurrences of green with violet.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 violet
2 blue
3 hot pink
4 orange
5 yellow
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: q
See you later!
▶️ happiness.txt revisited example (existing file, replace, overwrite, invalid commands)
>>> notes()
Enter the filename: happiness.txt
Found file happiness.txt...
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 things that can make people happy
2 warm cookies
3 hugs
4 reality tv shows that are really bad
5 sitting near a crackling fire on a cold night
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: really
Please enter the replacement word: really really
Replaced 1 occurrences of really with really really.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 things that can make people happy
2 warm cookies
3 hugs
4 reality tv shows that are really really bad
5 sitting near a crackling fire on a cold night
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: o
Please enter the line number to be overwritten: 25
Sorry, the file happiness.txt does not contain line number 25.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: rep
Please enter the word to be found: 6
Please enter the replacement word: six
No occurrences of 6 found.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: o
Please enter the line number to be overwritten: 5
Please enter your note: hot chocolate
Overwriting line 5 with "hot chocolate".
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: r
File contents:
1 things that can make people happy
2 warm cookies
3 hugs
4 reality tv shows that are really really bad
5 hot chocolate
--------------------
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: go away
Sorry, that was not a valid command.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: done
Sorry, that was not a valid command.
File options: [r]ead, [d]elete, [i]nsert, [a]ppend, [o]verwrite, [rep]lace, [q]uit: quit
See you later!
Your functions must satisfy these constraints to earn full credit:
Remember that every function you write must include a documentation string (docstring) that describes what it does.
You must indicate any sources consulted when writing the program
All your variables are descriptively named
Your code works correctly without crashing and creates the relevant file contents
Your code works as our examples above do, and on other, more complex examples as well
Your submission documents your testing of your notetaker program (more details below)
Before starting this assignment, make sure to read through the sample runs above so you have a sense of what this program should be able to accomplish.
Write your functions below, one by one, testing as you go.
Functions you will write:
1. The getInfoFromUser function asks the user for information, using the provided prompt, and returns the string response with leading and trailing white space removed.
▶️ click to see getInfoFromUser examples
>>> getInfoFromUser('Your fave color? ')
Your fave color? red
'red'
>>> getInfoFromUser('This is the prompt message ==> ')
This is the prompt message ==> Hi!
'Hi!'
>>> getInfoFromUser('Enter the filename: ')
Enter the filename: hello.txt
'hello.txt'
2. The create function creates a brand new, empty file. Note that this function does not return anything. Note that the examples below use os.listdir('.') which shows all the files in the current directory. This is used to show the existence of new files.
▶️ click to see create examples
>>> os.listdir('.')
['notetaker.py', 'example.txt']
>>> create('silly.txt')
This file does not exist, creating it for you now...
>>> os.listdir('.')
['notetaker.py', 'silly.txt', 'example.txt']
>>> create('jelly.txt')
This file does not exist, creating it for you now...
>>> os.listdir('.')
['notetaker.py', 'silly.txt', 'jelly.txt', 'example.txt']
3. showFile This function shows the contents of the given file, and includes line numbers for each line in the file. If the file is empty, then the message “:File is empty:” is printed.
▶️ click to see showFile examples
>>> showFile('example.txt')
File contents:
1 junk
2 are the best
3 fluffy soft pillows
4 things i need to do
5 my cs homework
6 go to math office hours
7 submit my work hours
8 watermelon sherbert
--------------------
>>> showFile('jelly.txt')
:File is empty:
--------------------
4. append This function adds a new note to the end of the existing file. Note that this function does not return anything but it does change the contents of the file.
▶️ click to see append examples
>>> append('jelly.txt')
Please enter your note: strawberry
>>> append('jelly.txt')
Please enter your note: peach
>>> showFile('jelly.txt')
File contents:
1 strawberry
2 peach
--------------------
5. insert This function adds a new note to the beginning of the existing file. Note that this function does not return anything but it does change the contents of the file.
▶️ click to see insert examples
>>> append('jelly.txt')
Please enter your note: strawberry
>>> append('jelly.txt')
Please enter your note: peach
>>> showFile('jelly.txt')
File contents:
1 strawberry
2 peach
--------------------
>>> insert('jelly.txt')
Please enter your note: jalapeno apricot
"jalapeno apricot" is now the first line of jelly.txt.
>>> showFile('jelly.txt')
File contents:
1 jalapeno apricot
2 strawberry
3 peach
--------------------
>>> insert('jelly.txt')
Please enter your note: lemon curd
"lemon curd" is now the first line of jelly.txt.
>>> showFile('jelly.txt')
File contents:
1 lemon curd
2 jalapeno apricot
3 strawberry
4 peach
--------------------
6. delete This function deletes the contents of an existing file. Note that this function does not return anything but it does change the contents of the file. The file still exists, it is merely empty.
▶️ click to see delete examples
>>> append('jelly.txt')
Please enter your note: strawberry
>>> append('jelly.txt')
Please enter your note: peach
>>> showFile('jelly.txt')
File contents:
1 strawberry
2 peach
--------------------
>>> delete('jelly.txt')
Contents of jelly.txt have been deleted.
>>> showFile('jelly.txt')
:File is empty:
--------------------
7. overwrite This function overwrites the specified line of the given file with a new note. If the specified line number is not in the file, then a message to that effect is printed. Note that this function does not return anything but it does change the contents of the file.
▶️ click to see overwrite examples
>>> showFile('example.txt')
File contents:
1 junk
2 are the best
3 fluffy soft pillows
4 things i need to do
5 my cs homework
--------------------
>>> overwrite('example.txt')
Please enter the line number to be replaced: 2
Please enter your note: sleepy sleepy stay awake
Overwriting line 2 with "sleepy sleepy stay awake".
>>> showFile('example.txt')
File contents:
1 junk
2 sleepy sleepy stay awake
3 fluffy soft pillows
4 things i need to do
5 my cs homework
--------------------
>>> overwrite('example.txt')
Please enter the line number to be replaced: 1
Please enter your note: double rainbow
Overwriting line 1 with "double rainbow".
>>> showFile('example.txt')
File contents:
1 double rainbow
2 sleepy sleepy stay awake
3 fluffy soft pillows
4 things i need to do
5 my cs homework
--------------------
8. replace This function replaces all occurrences of a specified word in the given file with specified replacement word. If the specified word does not occur in the file, then a message to that effect is printed. Tracks and reports the total number of replacements. Note that this function does not return anything but it does change the contents of the file (if the given word occurs in the file).
Hint: The split method can convert a string into a list of strings.
Hint: The join string method can be used to glue a list of strings together, using the string to which join is applied as a separator.
>>> ':'.join(['bunny','cat','dog'])
'bunny:cat:dog'
>>> ' '.join(['bunny','cat','dog'])
'bunny cat dog'
>>> ''.join(['bunny','cat','dog'])
'bunnycatdog'
>>> 'X'.join(['bunny','cat','dog'])
'bunnyXcatXdog'
▶️ click to see replace examples
File contents:
1 yellow ducks
2 green frogs
3 white bunnies
4 blue frogs
5 blue ducks
6 froggy
7
--------------------
>>> replace('animals.txt')
Please enter the word to be found: ducks
Please enter the replacement word: fish
Replaced 2 occurrences of ducks with fish.
>>> showFile('animals.txt')
File contents:
1 yellow fish
2 green frogs
3 white bunnies
4 blue frogs
5 blue fish
6 froggy
7
--------------------
>>> replace('animals.txt')
Please enter the word to be found: frog
Please enter the replacement word: toad
No occurrences of frog found.
>>> replace('animals.txt')
Please enter the word to be found: frogs
Please enter the replacement word: toads
Replaced 2 occurrences of frogs with toads.
>>> showFile('animals.txt')
File contents:
1 yellow fish
2 green toads
3 white bunnies
4 blue toads
5 blue fish
6 froggy
7
--------------------
9. notes For the notes function, you are provided with a skeleton below, but you need to complete it. This function contains a while loop that continually asks the user for a desired action until the user enters “q” or “quit”, and then, at that point, the program ends. For full credit, your input prompts must match ours as shown in the examples above such as the three Sample Runs of the notetaker program above.
▶️ click to see the provided notes template
# Complete the notes function
def notes():
'''Allows the user to edit notes in a file. The user has the following
options:
- if the file isn't found, then create an empty one
Operations that can be performed on a file:
- show the entire file
- delete the contents of the file
- insert a new note at the beginning of the file
- append a new note to the end of the file
- overwrite a line in the file
- replace occurrences of one word with another
'''
filename = getInfoFromUser('Enter the filename: ')
try:
with open(filename, 'r') as fread:
print(" Found file {}...".format(filename))
except FileNotFoundError:
create(filename)
done = False
while not done:
# flesh out the body of this loop
pass # delete this pass when you complete the while loop
Your test evidence should demonstrate that your program works, not just that you ran it. Screenshots are useful to show the contents of a file before and after changes are made to it. Show the file before the operation, perform the operation, and show the result afterward.
Your testing document must include at least the 7 tests below. Clearly number your tests to correspond to the tests 1 - 7 in your document. If you have ideas for other tests, please free to include them, starting with number 8, and describe what you are testing. All of your tests should start with the same original text file provided for you called ogtest.txt. In other words, test 3 below starts with ogtest.txt, but makes changes to the file. When you start test 4, start with the original, unedited ogtest.txt, rather than the edited version that results from test 3.
This screenshot shows that delete works properly. It shows the original contents of the jelly.txt file, and then deletes the contents, and the shows that the file is now empty.
Successfully open and display the contents of a file
Demonstrate that file deletion works (see example above demonstrating delete function):
Edit a file by appending and inserting (separate operations) to the file, display the changed contents of the file
Edit a file with multiple (at least three) insertions, and display the changed contents of the file
Test the overwrite function, demonstrate that it works with at least two overwrites of different line numbers in the file, also show that it works correctly when a line number that is not in the file is given.
Test the replace function, demonstrate that it works with repeated replacements of different words, also show that it works properly if given a word that is not in the original file.
Show that the notes function works even if the user gives an unrecognized command.
Your testing file should make it clear that you've thought carefully about how to test the functions in this assignment, and you provide evidence that demonstrates proper functionality in various scenarios.
Make sure that your python file:
Starts with a comment indicating your name, the date, and any collaborators
Save your python file as username_notetaker.py, e.g, this is how Beyoncé Knowles would name her file: bk101_hw7_notetaker.py
Each function has a docstring
Submit your two separate files (Beyoncé would submit bk101_hw7_notetaker.py and bk101_hw7_testing_notetaker.txt) in gradescope