I've been programming for nearly 2 years, but I have 5 months python experience. I'm working on an app in tkinter, and I am at the point where I want to delete every singular element inside a .txt file if a button is pressed.
I tried this:
with open("path/to/file", "r+") as file:
lines = file.readlines()
file.seek(0)
for i in lines:
if i.strip() != "specific_element":
file.write(str("specific_element"))
i.truncate()
in my other project, but the only problem here is you have to specify the element you'd like to remove, which is in my case, irrelevant. I want all elements to be removed from a txt file.