r/PythonLearning • • 2d ago

Help Request Can't remove specific text in txt file.

rom tkinter import *


def add_product():
    new_product = name_entry.get()
    with open("list of products.txt", "a") as file:
        new_product_name = name_entry.get()
        new_product_ID = id_entry.get()
        new_product_category = category_entry.get()
        new_product_price = price_entry.get()
        new_product_quantity = quantity_entry.get()
        new_product_expiry = expiry_entry.get()
        file.write(f"{new_product_name}\n")
        name_entry.delete(0, END)
        id_entry.delete(0, END)
        category_entry.delete(0, END)
        price_entry.delete(0, END)
        quantity_entry.delete(0, END)
        expiry_entry.delete(0, END)


def remove_product():
    name_data = name_entry.get()
    id_data = id_entry.get()
    category_data = category_entry.get()
    price_data = price_entry.get()
    quantity_data = quantity_entry.get()
    expiry_data = expiry_entry.get()
    with open("list of products.txt", "r") as file:
        products = file.readlines()
    with open("list of products.txt", "w") as file:
        for line in products:
            print(line)
            name = line
            if name != name_data.strip():
                file.write(name)

everytime i try to enter a product "apple" to delete, it will never be deleted from the "list of products.txt"

this is what is included inside products.exe:
apple
pear
pineapple
1 Upvotes

4 comments sorted by

4

u/Refwah 2d ago

Well you’re stripping the name_data but not the name

2

u/tiredITguy42 2d ago edited 2d ago

The exact questiom was here yesterday. Did you delete it and repost?

-1

u/Broad_Revolution6591 2d ago

yeah, i thought it wasnt clear enough