• Home
  • Get Started
  • Updates
  • Support
  • Shop
  • Pricing
  • AI News
Get Started
  • Login
  • Register
Neuraldemy
Cart / 0.00$

No products in the cart.

No Result
View All Result
Get Started
Neuraldemy
Get started
Home Free

Notes App In Python: Complete Tutorial

Amritesh Kumar by Amritesh Kumar
November 30, 2023 - Updated on August 1, 2024
in Python
Reading Time: 10 mins read
A A
Notes App In Python: Complete Tutorial

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:

  1. How to work with files.
  2. How to check if certain file types exist on the system.
  3. How to work with OS and glob modules.
  4. How to find files of a certain type.
  5. How to accept user input, and validate them based on stored files.
  6. How to change user input based on project requirements.
  7. How to allow users to create, write, update and delete their notes and data.
  8. How to delete a word and replace it with a new word or a line in text from any position.
  9. How to work with the cursor so that users can edit their notes.
  10. How to insert new lines anywhere in the text.
  11. How to make a user-friendly app by handling errors.
  12. How to use while loop and for loop with file management.
  13. and many things.

Prerequisites:

  1. 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.

Tags: Python ProjectsPython Tutorial
Previous Post

E-commerce App In Python – User Facing

Next Post

Create A To-Do-List App In Python: Complete Tutorial

Amritesh Kumar

Amritesh Kumar

I believe you are not dumb or unintelligent; you just never had someone who could simplify the concepts you struggled to understand. My goal here is to simplify AI for all. Please help me improve this platform by contributing your knowledge on machine learning and data science, or help me improve current tutorials. I want to keep all the resources free except for support and certifications. Email me @amriteshkr18@gmail.com.

Related Posts

pickle module in python

Pickle Module In Python: A Comprehensive Guide for Beginners

Python Os Module

Python OS Module: A Comprehensive Guide for Beginners

Install Miniconda on Windows (The EASIEST Way! )

TensorFlow Simplified Guide For Beginners

Create A To-Do-List App In Python: Complete Tutorial

E-commerce App In Python – User Facing

Next Post
Create A To-Do-List App In Python

Create A To-Do-List App In Python: Complete Tutorial

Why AI Won’t Replace Your Jobs!!

Why AI Won’t Replace Your Jobs!!

AI & Jobs: Understanding How Things May Actually Happen

AI & Jobs: Understanding How Things May Actually Happen

  • Customer Support
  • Get Started
  • Ask Your ML Queries
  • Contact
  • Privacy Policy
  • Terms Of Use
Neuraldemy

© 2024 - A learning platform by Odist Magazine

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Get Started
  • Updates
  • Support
  • Shop
  • Pricing
  • AI News
  • Login
  • Sign Up
  • Cart
Order Details

© 2024 - A learning platform by Odist Magazine

This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
0