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