0

Currently, the program erases all entries that have been saved to a text file. This is done when the user calls on the clear_path function by clicking on a listBox item then clicking the clear button.

from Tkinter import *
import os

root = Tk()
root.title("MyPath")
#root.iconbitmap(default='SpSoLogo.png.ico')

class MainGui:
    def __init__(self, master):
        self.mypathLabel = Label(master, text="Copy and Paste (Ctrl-V) your filepath here:")
        self.mypathEntry = Entry(master, bg="black", fg="white", relief=SUNKEN)

        self.mypathSaveButton = Button(master, text="Save Path", bg="black", fg="white", command=self.save_path)
        self.mypathLoadButton = Button(master, text="Load Paths", bg="black", fg="white", command=self.load_paths)
        self.mypathClearButton = Button(master, text="Clear Path", bg="black", fg="white", command=self.clear_path)

        self.mypathList = Listbox(master, bg="black", fg="white")
        self.mypathScroll = Scrollbar(master, orient=HORIZONTAL, bg="black")

        self.mypathScroll.config(command=self.mypathList.xview)

        self.mypathLabel.pack()
        self.mypathEntry.pack()

        self.mypathSaveButton.pack()
        self.mypathLoadButton.pack()
        self.mypathClearButton.pack(side=BOTTOM)

        self.mypathList.pack()
        self.mypathList.bind("<Double-Button-1>", self.open_path, self.clear_path)

        self.mypathScroll.pack()

def load_paths(self):
    with open("favorites.txt", "r") as f:
        for line in f:
            self.mypathList.insert(END, line)

def clear_path(self):
    if self.mypathList.curselection():
        getOut = self.mypathList.curselection()
        self.mypathList.delete(getOut)
    else:
        self.mypathList.delete(0, 'end')

    #####Everything Erases from the text file#####

    badPath = [self.mypathList.curselection()]
    f = open("favorites.txt", "r")
    lines = f.readlines()
    f.close()

    f = open("favorites.txt", "w")
    for line in lines:
        if line == badPath:
            f.delete(line)
    f.close()

def save_path(self):
    fp = self.mypathEntry.get()
    scribe=open("favorites.txt", "a")
    scribe.write(fp + '\n')
    self.mypathEntry.delete(0, 'end')
    self.mypathList.insert(1, fp)


def open_path(self, event):
    list_item = self.mypathList.curselection()
    fp = self.mypathList.get(list_item[0])
    os.system('Explorer "%s"' % fp)
##        print(fp)
##        try:
##            with open(fp, 'r') as result:
##                print(result.read())
##        except Exception as e:
##            print(e)    

MainGui(root)
root.mainloop()

Q.) Why does this happen and how do I change it to only delete the selected item?

mattyb
  • 11
  • 1
  • 8
  • Could you gut this down to a [mcve]? Is the UI even relevant, for instance? And note that `f = open("favorites.txt", "w")` will open the file for writing, *deleting everything in it*. – jonrsharpe Jun 15 '16 at 15:15
  • the `clear_path` function calls on multiple elements throughout the code so i figured I'd provide everything. However, I'll see if I can cut it down. If I open the file for `a` appending it doesnt erase anything from the txt file. Is there another option for editing the text file? – mattyb Jun 15 '16 at 15:24
  • Erasing lines from a text file in-place is basically impossible. The easiest thing to do is to read in the whole file, edit it in memory, then write out the result. – jonrsharpe Jun 15 '16 at 15:25
  • hmmm thats a good idea thank you! I'll try it out. – mattyb Jun 15 '16 at 15:30

0 Answers0