I'm hoping somebody here can help me figure this out
Basically what I'm trying to do is delete everything in the Windows 10 temp folder
if there is an item or more in the folder this program works perfectly
What it supposed to do is delete all the items and then close the top level window
which it does
The problem is if the folder is empty
the top level window will not close
I'm having a difficult time figuring why this is happening
ps
I'm also using messagebox.askyesno()
if you select no it will close the top level window
every time whether the folder is empty or not
If you select yes hence I have the problem
again if the folder is empty the top level window will not close
if the folder has items in it the top level window will close
Any help would be greatly appreciated
I am missing something here I just can't see it
Here is a sample of my script
Basically what I'm trying to do is delete everything in the Windows 10 temp folder
if there is an item or more in the folder this program works perfectly
What it supposed to do is delete all the items and then close the top level window
which it does
The problem is if the folder is empty
the top level window will not close
I'm having a difficult time figuring why this is happening
ps
I'm also using messagebox.askyesno()
if you select no it will close the top level window
every time whether the folder is empty or not
If you select yes hence I have the problem
again if the folder is empty the top level window will not close
if the folder has items in it the top level window will close
Any help would be greatly appreciated
I am missing something here I just can't see it
Here is a sample of my script
def windows_temp_data(): # temp cleans Windows temp folder top = Toplevel() top.geometry("677x386") # Size of the window top.title('Command Prompt In Tkinter') # Listbox -shows program file description- label_header = Label(top, text='Program information', font=('Arial bold', 13)) label_header.place(x=150, y=10) my_listbox = tk.Listbox(top, bg='black', fg='white', font=('Arial Bold', 13)) my_listbox.pack(expand=True, fill=tk.BOTH) # x=justify left to right scrollbar = Scrollbar(my_listbox) scrollbar.pack(side=RIGHT, fill=Y) my_listbox.config(yscrollcommand=scrollbar.set) scrollbar.config(command=my_listbox.yview) # username = getpass.getuser() # current location of the users temp folder temp_dir_wtd = f'C:\\Windows\\Temp' files_wtd = os.listdir(temp_dir_wtd) for file_wtd in files_wtd: filepath = os.path.join(temp_dir_wtd, file_wtd) my_listbox.insert(END, file_wtd) response = messagebox.askyesno('Maintenance', 'Would you like to remove these files') if response is True: print('hi') for file_wtd in files_wtd: filepath = os.path.join(temp_dir_wtd, file_wtd) try: if os.path.isfile(filepath): os.unlink(filepath) elif os.path.isdir(filepath): shutil.rmtree(filepath) top.after(2000, lambda: my_listbox.insert(END, filepath)) my_listbox.delete(0, END) top.after(2000, top.destroy) except (Exception,): print('ko') top.after(2000, lambda: my_listbox.insert(END, filepath)) my_listbox.delete(0, END) top.after(2000, top.destroy) elif response is False: messagebox.showinfo('information', 'very well I will leave it alone') top.after(1000, top.destroy) top.mainloop()