This unique notes app in Python allows users to create and manage notes on their computers. It’s user-friendly and tested. In case you find any bugs feel free to report them. Try on your own and see what you can achieve. Please note this app is the result of my own thought process. Unique suggestions and improvements are welcome.
In this project you will learn:
- How to work with files.
- How to check if certain file types exist on the system.
- How to work with OS and glob modules.
- How to find files of a certain type.
- How to accept user input, and validate them based on stored files.
- How to change user input based on project requirements.
- How to allow users to create, write, update and delete their notes and data.
- How to delete a word and replace it with a new word or a line in text from any position.
- How to work with the cursor so that users can edit their notes.
- How to insert new lines anywhere in the text.
- How to make a user-friendly app by handling errors.
- How to use while loop and for loop with file management.
- and many things.
Prerequisites:
- Python Functions & OS module
Please note there are some limitations in the update_note()
function due to the operating system. Rest everything works fine. It’s a great project to learn the os module, file r,w, r+ and more. Make sure to code along to build your project. Open your Jupyter Notebook and write as you progress. Jupyter Notebook is a great way to validate your code’s output and error.
Let’s import libraries and write a function to check notes and print the file name.
import os
import glob
def my_notes():
if len(glob.glob("*.txt")) == 0:
print("You don't have any notes")
else:
print("Your notes are:\n")
for file in os.listdir():
if file.endswith(".txt"):
print(file)
Code language: Python (python)
Allow the user to create a note and check if it exists already to validate and offer the best user experience.
def create_note():
note_name = input("Please enter a note name\n")
if ".txt" in note_name:
note_name = note_name
else:
note_name = note_name + ".txt"
try:
with open(note_name, 'x') as note:
note.write('')
except FileExistsError:
print('Ohho! this note name already exist. Try again.')
else:
print("Your note created. You can start writing now")
my_notes()
Code language: Python (python)
Allow the user to write something to the selected note file.
def write_note():
if len(glob.glob("*.txt")) == 0:
print("You don't have any notes")
else:
notename = input('Enter the name of the note to work with\n')
if ".txt" in notename:
notename = notename
else:
notename = notename + ".txt"
if not os.path.exists(notename):
print("Note does not exist. Please create a new note.")
else:
with open(notename, 'a') as note:
while True:
take_note = input("Please enter your texts or Q/q to exit \n")
if take_note in ["q", "Q"]:
break
else:
note.write(take_note + "\n")
with open(notename, 'r') as note:
content = note.read()
print("your updated note is: ")
print(content)
Code language: Python (python)
Allow user to delete a single note:
def delete_note():
if len(glob.glob("*.txt")) == 0:
print("You don't have any notes")
else:
filename = input("Enter the note name to be deleted forever\n")
if ".txt" in filename:
filename = filename
else:
filename = filename + ".txt"
if os.path.exists(filename):
os.remove(filename)
print("\nYour note has been deleted")
else:
print('This note does not exist')
Code language: Python (python)
Allow users to delete all notes or view all notes:
def delete_all():
if len(glob.glob("*.txt")) == 0:
print("You don't have any notes")
else:
for file in os.listdir():
if file.endswith(".txt"):
os.remove(file)
print("All your notes has been deleted.")
def view_all():
if len(glob.glob("*.txt")) == 0:
print("You don't have any notes")
else:
print(f"Your notes overviews\n")
for file in os.listdir():
if file.endswith(".txt"):
with open(file) as f:
print(f"Your {file} note contains: \n{f.read()}")
Code language: Python (python)
Now this is the toughest part. Once the user has written something to the note how you are going to allow them to update that particular note? Here is what you can achieve to a great extent:
def update_note():
view_all()
choice = input("choose a note you want to update\n")
if ".txt" in choice:
choice = choice
else:
choice = choice + ".txt"
if not os.path.exists(choice):
print("Note does not exist. Please create a new note.")
else:
print("\nYour note choice is:\n", choice)
print("\n")
print("Enter your preference:\n")
print("Clear note content: 1")
print("Rewrite note content: 2")
print("Delete a word/replace a word/insert word or a sentence at a position: 3")
try:
action = int(input("Enter a choice between 1, 2, 3\n"))
except ValueError:
print("Please enter the mentioned choice only\n")
else:
if action not in [1, 2, 3]:
print('Please enter a valid value\n')
elif action == 1:
with open(choice, 'w') as file:
file.write(' ')
print("Your note content is cleared.")
elif action == 2:
with open(choice, 'w') as note:
while True:
take_note = input("Please enter your texts or Q/q to exit \n")
if take_note in ["q", "Q"]:
break
else:
note.write(take_note + "\n")
with open(choice) as note:
print("your updated note is: ")
print(note.read())
elif action == 3:
with open(choice, "r+") as file:
content = file.read().split()
print("Content length is:", len(content))
print("Your content is:", ' '.join(content))
try:
cursor_input = int(input("Enter the word position to replace a word or insert a sentence or enter above range for adding something to end \n."))
cursor = file.seek(cursor_input - 1)
print("\nYour modified position now is:", cursor_input)
try:
content.pop(cursor)
except IndexError:
print("Please enter the value within range to update word or add something new else it will be added to the end")
except ValueError:
print("Enter number only within range")
else:
replacement_input = input("Enter the word/sentence to added at the position. Hit enter if you just want to delete the word at that position.")
content.insert(cursor, replacement_input)
content = ' '.join(content)
print(content)
with open(choice, "w") as file:
file.write(content)
Code language: Python (python)
Thanks for reading. If you want to build another app then check out this post that will teach you how you can build your e-commerce app in Python.