-1

I am trying create a python script where it continously delete a file once I drop it in the Trash can on MacOs. When I run the script, it runs successfully but does not delete the items in the trash.

This is the code. How can I get it to continue to delete:

#add all the import modules

 import send2trash
 import sched
 import time
 import os

#create variable for auto delete

event_schedule = sched.scheduler(time.time, time.sleep)

#create function to run the auto delete automation

def auto_delete():

#get all the files in trash and delete

 trash_path = os.path.expanduser("~/.Trash")
 trash_items = os.listdir(trash_path)
 for item in trash_items:
    item_path = os.path.join(trash_path, item)
    send2trash.send2trash(item_path)
event_schedule.enter(30, 1, auto_delete)

#use try and accept and call the function to delete the data inside trash
try:
    trash_path = os.path.expanduser("~/.Trash")
    trash_items = os.listdir(trash_path)
    for item in trash_items:
        item_path = os.path.join(trash_path, item)
        send2trash.send2trash(item_path)
    print('Trash has been emptied')
except:
    print("The trash is already empty")

event_schedule.enter(30, 1, auto_delete) event_schedule.run()

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Seyi
  • 1
  • 2
  • Are you sure `send2trash` is providing the functionality you seem to require in this instance? Why not [actually *deleting* the files from `~/.Trash` using `os.remove()` instead?](https://stackoverflow.com/questions/57130303/how-to-permanently-delete-a-file-in-python-3-and-higher) – esqew Aug 14 '23 at 03:30
  • okay, I will try that to see if it removes the files in the trash. – Seyi Aug 14 '23 at 11:51

0 Answers0