Python Forum
Deleting Windows temp folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting Windows temp folder
#1
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


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()
Reply


Messages In This Thread
Deleting Windows temp folder - by Raysz - Mar-30-2024, 06:29 PM
RE: Deleting Windows temp folder - by deanhystad - Mar-30-2024, 06:52 PM
RE: Deleting Windows temp folder - by Raysz - Mar-31-2024, 11:04 AM
RE: Deleting Windows temp folder - by deanhystad - Mar-31-2024, 12:32 PM
RE: Deleting Windows temp folder - by Raysz - Mar-31-2024, 06:10 PM
RE: Deleting Windows temp folder - by deanhystad - Apr-01-2024, 02:47 PM
RE: Deleting Windows temp folder - by Raysz - Apr-01-2024, 06:56 PM
RE: Deleting Windows temp folder - by Raysz - Apr-02-2024, 12:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to add Python folder in Windows Registry ? Touktouk 1 334 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 640 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 802 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,593 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pyspark creating temp files in /tmp folder aliyesami 1 5,154 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,473 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  How to save Matplot chart to temp file? Morkus 2 4,609 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,523 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,174 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  size of a windows folder metro17 1 1,809 Nov-03-2019, 06:20 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020