r/PythonLearning • u/Broad_Revolution6591 • 10d ago
Help Request are there anyway to reject a string entry if it already existed inside the txt file?
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()
messagebox.askokcancel(title="submit a new product?", message="Submit a new product?")
file.write(f"{new_product_name}, {new_product_ID}, {new_product_category}, {new_product_price}, {new_product_quantity}, {new_product_expiry}\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)Hi guys, are there anyway to refuse an entry that has already existed inside the file? for example if biscuit is already inside the txt file, I entered it again and it will refuse it and not add it to the txt file.
2
u/8dot30662386292pow2 10d ago
Don't treat your file as a random access memory. Read it all to a list while the program is running. Edit the list and use that when checking duplicates. In the end, write the list back to the file.
2
u/atarivcs 10d ago
What is your actual question?
Are you asking how to check if a file contains a string?
Or are you asking how to reject input?
2
u/Immediate-Soup-6344 10d ago
You could load the entire file into ram like everyone else is saying, or you could just iterate over it once and break out of the loop once you find a duplicate, and only add the new thing if the loop finishes. You could keep track of where each letter of the alphabet begins (if your file is sorted alphabetically) and only search the slice that starts with the same letter. It alllllllll depends on how efficient you want to be
2
u/omgseriouslynoway 10d ago
Also don't forget to make it case insensitive so you don't get biscuit and Biscuit as separate entries
2
1
u/GreatGameMate 10d ago
Create some function called check duplicates. It will loop through the text file and check for string the user inputs, if it already exists return false, otherwise return true.
What makes every product unique? The name or the id?
1
1
u/Classic-Rate-5104 10d ago
It isn't a good idea to put this kind of info into a text file. It will slow down if the number of products increases. You cannot randomly access elements. Removing or replacing an element is only possible by copying the whole file
1
u/Avatarbroskib1 10d ago
can't you write an if statement saying if input entered in f.read():
and then like catch it with that
1
u/Zeak3D 10d ago
If I were you I would structure the product data in a dictionary, and before adding the product, I would check if the key already exists. For this reason I would also save the data in a json file instead of txt.
Also, I’m not an expert in tkinter (which this seems to be?) but should you not have some sort of if-statement using the messagebox?
1
u/ClearBreakfast2308 10d ago
Plate, paper,10 inch, Paper plate 10 inch, 10 inch Paper plate
In addition to case differences and maybe symbol or abbreviation for inch.....
1
u/DoKeMaSu 7d ago
This is wrong on so many levels. Remember that the computer cannot search an entry in a text file, it has to read the entire file from the first line every time you look for an entry. Horribly inefficient.
Either read everything into memory or use a data base format.
5
u/notyboy01 10d ago
Read the file into a set first.